flutterbluetooth

Different values being returned on Flutter Blue Plus


I'm using the example code from the flutter_blue_plus. I can connect to the device and read the data stream as an array:

[90, 0, 0, 6, 13, 0, 0, 0, 0, 0, 0, 0, 37, 5, 0, 0, 0, 0, 0, 0, 0, 0, 45, 5, 49, 0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 91, 118, 90]

void onNewReceivedData(List<int> data) {
_numberOfMessagesReceived += 1;
_receivedData.add( "$_numberOfMessagesReceived: $data" as int);
if (_receivedData.length > 5) {
  _receivedData.removeAt(0);
}
print(_receivedData.join("\n"));
refreshScreen();

}

But they are not the correct numbers. I've contacted the manufacturer of the Bluetooth module I'm trying to use, and he told me It's correct and sent me this:

Low byte 45 = 0x2D

High byte 05 = 0x05

0x052D = 1325 = 13.2V

The manufacturer is Chinese, and we are stuck in a big language barrier here; I've been using google translate to translate his emails, but most of it makes no sense to me. Do you happen to know what he is talking about?

Thanks! Dan


Solution

  • I didn't understand the part that was confusing you. You're trying to build a value from a 16-bit little-endian value. I'm guessing the result is then in centivolts (1/100 volt). (It would be much easier to answer if you would include what service this is using.)

    If you have already pulled out the values, 45 and 5, then this is how you turn that into a complete value:

    const low = 45;
    const high = 5;
    const volts = ((high << 8) + low) / 100; // 13.25
    

    This shifts high left 8 bits (to make it the most-significant byte), adds low (as the least-significant byte), and divides the entire value by 100 to get volts.

    There is no significance to whether these values are represented in hexadecimal or decimal. Those are just different ways of writing down numbers. It has no impact on what the numbers actually are.

    See also Dart Convert two Uint8 to Uint16 that are little endian for how to do this directly out of a Uint8List.