Search code examples
flutterfloating-pointbluetooth-lowenergydeviceieee-11073

get values from ble device (Understanding Data Conversion (ieee-11073 16 bit) )


Please, I am a beginner in this field and I need someone to explain to me in detail through my questions (it's for my Final Year Project), please. I used the 'flutter_reactive_ble' package to integrate my medical device into my Flutter application to read blood pressure, and here is what I received as a result: D/BluetoothGatt(21288): setCharacteristicNotification() - uuid: 00002a35-0000-1000-8000-00805f9b34fb enable: true I/flutter (21288): Received raw data for blood pressure measurement: [222, 246, 244, 22, 243, 255, 7, 228, 7, 1, 20, 8, 43, 0, 178, 242, 1, 128, 0]. Knowing that in the device documentation it indicates that the value, for example, of the systolic is in the format: sfloat (2 bytes), these two: 246, 244 are responsible for the value of the systolic.

  1. My first question is whether this array is in sfloat format or in bytes or in decimal?? I did not understand well,

  2. My second question is do I need to convert these two values: 246, 244 to sfloat to obtain the real value? Or are these two values already in sfloat format and do I need to convert them to decimal? I didn't understand anything,

  3. My third question how do I convert them? Do I convert each value separately or how? Please,

Can someone explain to me how to convert these two values step by step to obtain the final systolic value, which should be 127.0 so that I can understand? I want to understand the conversion process thoroughly so that I can write my code.

I have done some research but I haven't found anything that explains well, especially since I am very much a beginner in this field.


Solution

  • Description of the digital format you need is here: https://en.everybodywiki.com/SFLOAT-Type_Data_Type

    Dart code:

    import 'dart:math';
    import 'dart:typed_data';
    
    void main(List<String> arguments) {
      List<int> packet = [222, 246, 244, 22, 243, 255, 7, 228, 7, 1, 20, 8, 43, 0, 178, 242, 1, 128, 0];
      print(sFloatToInt(packet));
    }
    
    double sFloatToInt(List<int> packet) {
      final data = ByteData.sublistView(Int8List.fromList(packet));
      const startByte = 1;
      int value = 0;
    
      value = data.getInt16(startByte, Endian.little);
      int mantissa = value & 0xfff;
    
      if (mantissa >= 0x0800) {
        mantissa = -((0x0FFF + 1) - mantissa);
      }
    
      int exponent = value >> 12;
    
      if (exponent >= 0x0008) {
        exponent = -((0x000F + 1) - exponent);
      }
    
      double magnitude = pow(10.0, exponent).toDouble();
      double measurement = mantissa * magnitude;
    
      return measurement;
    }
    

    Mathematical calculation, perhaps it will be clearer enter image description here