Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlutterBlue Characteristics

I am developing a bluetooth app that connects to a fitness watch. This is my first time working with bluetooth. I managed to connect my app with the device using the brilliant FlutterBlue library.

However I am unable to make sense of the results I get from reading. This is how I read the characteristics:

_readCharacteristic(BluetoothCharacteristic c) async {
    var results = await widget.device.readCharacteristic(c);
    print("${results.toList()}");
    //setState(() {});
  }

This is the result:

[7, 133, 0, 0, 1, 0, 0, 124, 92, 1]

I have no idea what these numbers mean or what I am supposed to do with them.

like image 239
spongyboss Avatar asked Jul 07 '26 12:07

spongyboss


1 Answers

From the documentation:

var characteristics = service.characteristics;
for(BluetoothCharacteristic c in characteristics) {
    List<int> value = await device.readCharacteristic(c);
    print(value);
}

// Writes to a characteristic
await device.writeCharacteristic(c, [0x12, 0x34])

We can see that the library works on List(int) types, and that it sends/receives "32 bit" values.

It is most likely pairs of bytes being sent over, so 16 bit values represented as numbers in the list; these are characters. This means that you can send over characters with their utf8 representation.

In the above example, the characteristic is writing 0x12, then 0x34. In the link to the ascii character table, this translates to "(Device Control 2) (4)".

It is your job to decode them into characters, (UTF8) and also to encode them when sending them back to the watch. This is required by the watch's software, which can respond to certain characteristic writes depending on the received value.

You probably will have to do some digging into the documentation/bluetooth spec of the watch you are using.

Check out the UTF8Decoder class of dart:convert lib. It should help you translate it into human-readable text. If not, you will have to do some digging.

String decoded = UTF8Decoder().convert(value) // value == List<int>, Uint8List, etc.
print(decoded)
like image 54
Jeremi G Avatar answered Jul 10 '26 14:07

Jeremi G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!