3

I am sending this from my BLE device:

BTLEserial.print ("one");
BTLEserial.print (",");
BTLEserial.print ("two"); 

So what I am sending is: "one, two" and i am now trying to get this value but with my current code I get 54 and 44 instead and I do not quite know why.

I use this plugin: https://github.com/xabre/xamarin-bluetooth-le (Plugin.BLE)

This is how I read the data:

var adapter = CrossBluetoothLE.Current.Adapter;

await adapter.ConnectToDeviceAsync(mydevice);

var service = await mydevice.GetServiceAsync(Guid.Parse("6e400001-b5a3-f393-e0a9-e50e24dcca9e"));
var services = await mydevice.GetServicesAsync();

var RXcharacteristics = await service.GetCharacteristicAsync(Guid.Parse("6e400003-b5a3-f393-e0a9-e50e24dcca9e"));
var characteristics = await service.GetCharacteristicsAsync();

int whatResult = 0;
string valueone;
string valuetwo;

RXcharacteristics.ValueUpdated += (sender, e) =>
{
    var result = e.Characteristic.Value;

    foreach (var items in result)
    {
        if (whatResult == 0)
        {
            valueone = Convert.ToString(items);
            System.Diagnostics.Debug.Writeline(valueone);
            whatResult++;
        }
        else {
            valuetwo = Convert.ToString(items);
            System.Diagnostics.Debug.Writeline(valuetwo);
            whatResult = 0;
        }
    }

}; 

await RXcharacteristics.StartUpdatesAsync();

How come I cannot get the correct data from the result I am receiving? I also tried with var result = e.Characteristic.StringValue; but with the same result.

I googled and came across a person with the same issue: https://github.com/xabre/xamarin-bluetooth-le/issues/88

He for example said this, which showcase that my device has CanUpdate as true. And as I said above I succesfully get data from my code but I do not get the correct values.

The RXCharacteristic has CanWrite = false, CanRead = false, CanUpdate = true

If I use this app:

https://github.com/adafruit/Bluefruit_LE_Connect_v2

That is open source (coded with swift) I can succesfully get the correct value

Carlos Rodrigez
  • 1,347
  • 1
  • 15
  • 32

1 Answers1

3

The github documentation for this library indicates that the Value is a byte array

public override byte[] Value => _nativeCharacteristic.Value?.ToArray();

Which would mean that instead of using a foreach loop over each byte you should attempt to use a text encoding instead 1

var result = e.Characteristic.Value;
var str = System.Text.Encoding.UTF8.GetString(result,0,result.Length);

1. Andy0708, Fri Jan 06 2017, Oksana, "How convert byte array to string [duplicate]", Jul 25 '12 at 16:39, https://stackoverflow.com/a/11654825/1026459

Community
  • 1
  • 1
Travis J
  • 81,153
  • 41
  • 202
  • 273