There is a device that measures tire tread depth and air pressure.
I made a connection but I can't pull data. Can you direct me on how to do it?
I pulled data using Android DLLs but failed with plugin.ble.
Note: The state in the device object disconnect after a certain time. I do not know the reason.
Source code I wrote
Part-1
bluetoothBLE = CrossBluetoothLE.Current;
adapter = CrossBluetoothLE.Current.Adapter;
list = new ObservableCollection();
DevicesList.ItemsSource = list;
Part-2
adapter.ScanTimeout = 10000;
adapter.ScanMode = ScanMode.Balanced;
adapter.DeviceDiscovered += (obj, a) =>
{
if (!list.Contains(a.Device))
list.Add(a.Device);
};
await adapter.StartScanningForDevicesAsync();
Part-3
device = DevicesList.SelectedItem as IDevice;
await adapter.StopScanningForDevicesAsync();
var connectParams = new ConnectParameters(true, true);
await adapter.ConnectToDeviceAsync(device, connectParams);
Part-4
var xservices = await device.GetServicesAsync();
var xservice = await device.GetServiceAsync(xservices[0].Id);
var Characteristic = await xservice.GetCharacteristicsAsync();
var xxxcharacteristicread = await xservice.GetCharacteristicAsync(xitem.Id);
var c = await xxxcharacteristicread.ReadAsync();
Version of the Plugin: 2.1.1
Platform: iOS 10.1 / Android 7.0
Device: samsung note 5 /i Phone 7
Related
I get this exception since I started integrating Google Sheets and Firebase and Firestore. Despite all the tutorials and articles, non of the solutions worked with me.
my issue is when using getDocuments method for retrieving all the docs, the program throws an exception saying Error: Missing or insufficient permissions.
I already project created in Google Cloud, and I have created all the services needed and added all the APIs as instructed from Google Cloud website. Also, I have added oauthscopes needed. unfortunately, it did not work also.
My code:
function getRatingData() {
// Initialize firestore
const firestore = initFirestore();
// Initialize spread sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSpreadSheet = ss.getSheetByName("Sheet1");
// Get all docs
const docs = firestore.getDocuments("rating_debug");
for(var i = 0; i < docs.length; i++){
var array = [];
const lastRow = activeSpreadSheet.getLastRow();
// Get data field of each doc
var date = docs[i].fields["date"];
array.push(date);
var customerID = docs[i].fields["customerID"];
array.push(customerID.stringValue);
var mandoobID = docs[i].fields["mandoobID"];
array.push(mandoobID.stringValue);
var offerUniqueID = docs[i].fields["offerUniqueID"];
array.push(offerUniqueID.stringValue);
var message = docs[i].fields["message"];
array.push(message.stringValue);
var rating = docs[i].fields["rating"];
array.push(rating);
activeSpreadSheet.appendRow(array)[lastRow + 1];
}
}
function initFirestore(){
var config = {
"project_id" : "PROJECT-ID",
"private_key" : "PRIVATE-KEY",
"client_email" : "CLIENT-EMAIL",
};
return FirestoreApp.getFirestore(config.client_email, config.private_key, config.project_id);
}
My oauthScopes
"https://www.googleapis.com/auth/spreadsheets.readonly",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/compute",
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/cloud-platform.read-only",
"https://www.googleapis.com/auth/datastore",
"https://www.googleapis.com/auth/firebase",
"https://www.googleapis.com/auth/firebase.readonly",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/script.deployments",
"https://www.googleapis.com/auth/script.projects",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/gmail.addons.current.message.metadata",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/script.scriptapp",
"https://www.googleapis.com/auth/script.locale",
"https://www.googleapis.com/auth/documents.currentonly",
"https://www.googleapis.com/auth/spreadsheets.currentonly",
"https://www.googleapis.com/auth/presentations.currentonly",
"https://www.googleapis.com/auth/drive.addons.metadata.readonly",
"https://www.googleapis.com/auth/gmail.addons.current.action.compose",
"https://www.googleapis.com/auth/firebase.database"
This is my output for more clarification
photo of the output
I am struggling big time with generating QR barcodes as a byte[] or Stream (something that I can use on a XAML image source)
ZXING.NET
I've tried with Zxing.Net but I find the documentation is not great.
In fact, when installing in the xamarin forms class library I am able to compile, but as soon as I add some code to write barcodes I get a compilation error saying that
Can not resolve reference: `zxing`, referenced by `MyXamarinFormsClassLibrary`. Please add a NuGet package or assembly reference for `zxing`, or remove the reference to `Sasw.AforoPass`. 0
Something funky is going on with that library.
And I'm doing a simple example such as:
var options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Width = 250,
Height = 250
};
var writer = new BarcodeWriter<Image>();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
var result = writer.Write("foo");
MyImage.Source = result.Source;
QRCoder
It's another nuget library. I've successfully used for dotnet core applications, but it does not seem to be compatible with Xamarin Forms (or Mono, or whatever). It says that the platform is not supported. Probably because it uses System.Drawing.Common?
ZXing.Net.Mobile.Forms
I've used this other library which underneath it uses ZXing.Net. What I don't like is that I don't know if there's any way to generate qr codes without relying on Xaml or the ZXingBarcodeImageView.
I managed to generate QR Codes that way as a workaround, but I hit another wall. See https://github.com/Redth/ZXing.Net.Mobile/issues/908 in which I describe the problems I have to embed the ZXingBarcodeImageView in a carousel inside a popup.
So basically I wanted to go back to the roots, and simply have a working example with the latest version of ZXing.Net (or an alternative, if it exists) that I am able to use in Xamarin Forms.
Most of the examples I find talk about BarcodeWriter but there is no such a class anymore. There is a generic one BarcodeWriter<TUnknownType>and a BarcodeWriterGeneric but as I said, I could not compile anything using Zxing.Net library and with through ZXing.Net.Mobile the images I generate are always empty.
Any help or "modern" code sample (ideally with an alternative) would be much appreciated
UPDATE 1
In other words, I'm looking to have in Xamarin Forms something similar to this code that I had using QrCoder library.
public class QrCodeService
: IQrCodeService
{
public Stream GetQrCode(Guid id, string mimeType = "image/jpeg")
{
if (mimeType is null)
{
throw new ArgumentNullException(nameof(mimeType));
}
var qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode(id.ToString(), QRCodeGenerator.ECCLevel.Q);
var qrCode = new QRCode(qrCodeData);
var qrCodeImage = qrCode.GetGraphic(20);
var myImageCodecInfo = GetEncoderInfo(mimeType);
var myEncoder = Encoder.Quality;
var myEncoderParameters = new EncoderParameters(1);
var myEncoderParameter = new EncoderParameter(myEncoder, 50L);
myEncoderParameters.Param[0] = myEncoderParameter;
var stream = new MemoryStream();
qrCodeImage.Save(stream, myImageCodecInfo, myEncoderParameters);
stream.Position = 0;
return stream;
}
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
var encoders = ImageCodecInfo.GetImageEncoders();
foreach (var encoder in encoders)
{
if (encoder.MimeType == mimeType)
{
return encoder;
}
}
throw new KeyNotFoundException($"Encoder for {mimeType} not found");
}
}
The solution uses QrCoder library and it works fine for Xamarin Forms as follows.
private byte[] GetQrImageAsBytes()
{
var randomText = Guid.NewGuid().ToString();
var qrGenerator = new QRCodeGenerator();
var qrCodeData = qrGenerator.CreateQrCode(randomText, QRCodeGenerator.ECCLevel.L);
var qRCode = new PngByteQRCode(qrCodeData);
var qrCodeBytes = qRCode.GetGraphic(20);
return qrCodeBytes;
}
Here a working sample with this implementation
QrCoder library can do this, but instead of using as in the main page of the project:
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("The text which should be encoded.", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData); // This point onwards is problematic
Bitmap qrCodeImage = qrCode.GetGraphic(20); // This will throw not supported exception
Switch the last 2 lines to:
var qrCode = new PngByteQRCode(qrCodeData);
byte[] imageByteArray = qrCode.GetGraphic(20);
You'll get byte array instead, but you can convert it into Stream or whatever you like afterwards.
I am using Tessaract's Xamarin Forms Nuget(https://github.com/halkar/Tesseract.Xamarin), and am trying to scan a picture taken by the Android Device. This is the code that I am using:
private async System.Threading.Tasks.Task<string> OCRAsync(byte[] bytes)
{
TesseractApi api;
api = new TesseractApi(this, AssetsDeployment.OncePerInitialization);
await api.Init("bul");
await api.SetImage(bytes);
var detectedText = api.Results(PageIteratorLevel.Block);
result = string.Empty;
if (detectedText != null)
{
foreach (var annotation in detectedText)
{
result = FindWordInDictionary(annotation.Text);
}
}
return result;
}
The method is called from a synchronized method like this:
var task = OCRAsync(data);
result = task.Result;
Whenever the compiler gets to "await api.Init("bul");" the app freezes indefinitely. Do you know what may cause this problem? Thank you.
The problem was that I needed to give a file location in the .init function:
await api.Init(pathToDataFile, "bul");
I have some working code. but I am not entirely sure what the best practice is, while working with Task.WhenAll. I found different blog post's using different approaches, but none of them stating why they went with their approach. The only "good" posts I found, was using Task.WhenAll, to fetch similar data and combining the result. But my use-case is I several data-sources with different data, that I have to do some work on.
The approach that I went with was:
var weatherTask = GetWeatherAsync();
var locationTask = GetLocationAsync();
var driversTask = GetDriversAsync();
await Task.WhenAll(weatherTask, locationTask, driversTask)
var weather = await weatherTask;
var location = await locaitonTask;
var drivers = await driversTask;
Mainly because I liked the way it looked / readability. But I also had thought about using the following other approach:
var weatherTask = GetWeatherAsync();
var locationTask = GetLocationAsync();
var driversTask = GetDriversAsync();
await Task.WhenAll(weatherTask, locationTask, driversTask)
var weather = weatherTask.Result;
var location = locaitonTask.Result;
var drivers = driversTask.Result;
The thought behind this approach was, that the tasks already was done and it would be save to use the .Result. But I stuck with the first approach. Because I was not entirely sure about the overhead of 'await' (if any).
So I came here in hope of some smart people knew the answer.
*** Edit
I have looked at Awaiting multiple Tasks with different results
The answer to that post is:
You can also use Task.Result (since you know by this point they have all completed successfully). However, I recommend using await because it's clearly correct, while Result can cause problems in other scenarios.
But not if there is any performance, using one over the other.
******** Edit
Taking the example to the extreme. Look at these 2 loops.
for(int i=0; i< 1.000.000.000; i++)
{
var weatherTask = GetWeatherAsync();
var locationTask = GetLocationAsync();
var driversTask = GetDriversAsync();
await Task.WhenAll(weatherTask, locationTask, driversTask)
var weather = await weatherTask;
var location = await locaitonTask;
var drivers = await driversTask;
}
vs
for(int i=0; i< 1.000.000.000; i++)
{
var weatherTask = GetWeatherAsync();
var locationTask = GetLocationAsync();
var driversTask = GetDriversAsync();
await Task.WhenAll(weatherTask, locationTask, driversTask);
var weather = weatherTask.Result;
var location = locaitonTask.Result;
var drivers = driversTask.Result;
}
Would they both be just as quick? Or would the Task.Result be much faster?
So I made a not entirely accurate test. But gave me a small indicator of what I expected.
The code:
The result after 10 runs in a for loop:
Await was always slightly slower. But not with much. For 1.000.000, it wasn't really too big a issue. But if your scale is higher than that, using .Result after Task.WhenAll might be a good idea :)
This test was run while without closing browsers, visual studio and other apps. So is by no means a clean test. Just a quick attempt without using BenchmarkDotnet
Probably more like worst than best practice, but it seems like a viable alternative....
var weatherTask = Task.FromResult(new Weather());
var locationTask = Task.FromResult(new Location());
var driversTask = Task.FromResult(new Driver[0]);
var results = await Task.WhenAll(
Task.Run(async () => await weatherTask as object),
Task.Run(async () => await locationTask as object),
Task.Run(async () => await driversTask as object));
var weather = (Weather)results[0];
var location = (Location)results[1];
var drivers = (Driver[])results[2];
Data sending from google sheet:
Script using to send data to firebase from google sheet.
function writeData() {
var ss = SpreadsheetApp.openById("####");
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 1; i < data.length; i++) {
var department = data[i][0];
var year = data[i][1];
var course = data[i][2];
dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}
dataToImport[department][year][course][i] = {
course: data[i][3],
dateAdded: data[i][4],
fileSize: data[i][5],
fileType:data[i][6],
downloadLink: data[i][7],
};
}
var firebaseUrl = "https:url";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
base.setData("",dataToImport);
}
Data that is sent:
Q: My questions that since i had parent electrical and its two childs that were 1 and 2 and each child had a course dsp and aes respectively but only 1 child is send that is the second one.
Why first child is not sent to firebase ?
This code replacing previous entry with new entry. Replace this -
dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}
with this -
dataToImport[department] = dataToImport[department] || {};
dataToImport[department][year] = dataToImport[department][year] || {}
dataToImport[department][year][course] = dataToImport[department][year][course] || {}
As a side note to #ra89fi's answer (because I didn't pick up on the overwrite bug).
The line base.setData("",dataToImport); is setting all data at the root of your database ("") with the given data (dataToImport). Any other data in your database will be deleted.
Instead, you should use an update operation.
I unfortunately can't establish which version of the Firebase API you are using. It's not quite the REST API and not quite JavaScript. So here are some relevant documentation links.
REST Update Guide
REST Update Reference
JavaScript (Web) Update Guide
JavaScript (Web) Update Reference