Heart Rate Value in BLE

霸气de小男生 提交于 2021-01-28 11:13:17

问题


I am having a hard time getting a valid value out of the HR characteristics. I am clearly not handling the values properly in Dart.

Example Data:

List<int> value = [22, 56, 55, 4, 7, 3];

Flags Field: I convert the first item in the main byte array to binary to get the flags

22 = 10110 (as binary)

this leads me to believe that it is U16 (bit[0] is == 1)

HR Value:

Because it is 16 bit I am trying to get the bytes in the 1 & 2 indexes. I then try to buffer them into a ByteData. From there I get convert them to Uint16 with the Endian set to Little. This is giving me a value of 14136. Clearly I am missing something fundamental about how this is supposed to work.

Any help in clearing up what I am not understanding about how to process the 16 bit BLE values would be much appreciated.

Thank you.

  /*
Constructor - constructs the heart rate value from a BLE message
 */
  HeartRate(List<int> values) {
    var flags = values[0];
    var s = flags.toRadixString(2);
    List<String> flagsArray = s.split("");

    int offset = 0;

    //Determine whether it is U16 or not
    if (flagsArray[0] == "0") {
      //Since it is Uint8 i will only get the first value
      var hr = values[1];
      print(hr);
    } else {
      //Since UTF 16 is two bytes I need to combine them
      //Create a buffer with the first two bytes after the flags
      var buffer = new Uint8List.fromList(values.sublist(1, 3)).buffer;
      var hrBuffer = new ByteData.view(buffer);
      var hr = hrBuffer.getUint16(0, Endian.little);
      print(hr);
    }
  }

回答1:


Your updated data looks much better. Here's how to decode it, and the process you'd use to figure this out yourself from scratch.

Determine the format

The Bluetooth site has been reorganized recently (~2020), and in particular they got rid of some of the document viewers, which makes things much harder to find and read IMO. All the documentation is in the Heart Rate Service (HRS) document, linked from the main GATT page, but for just parsing the format, the best source I know of is the XML for org.bluetooth.characteristic.heart_rate_measurement. (Since the reorganization, I don't know how you can find this page without searching for it. It doesn't seem to be linked anymore.)

Byte 0 - Flags: 22 (0001 0110)

Bits are numbered from LSB (0) to MSB (7).

  • Bit 0 - Heart Rate Value Format: 0 => UINT8 beats per minute
  • Bit 1-2 - Sensor Contact Status: 11 => Supported and detected
  • Bit 3 - Energy Expended Status: 0 => Not present
  • Bit 4 - RR-Interval: 1 => One or more values are present

The meaning of RR-intervals is explained in the HRS document, linked above. It sounds like you just want the heart rate value, so I won't go into them here.

Byte 1 - UINT8 BPM: 56

Since Bit 0 of flags was 0, this is the beats per minute. 56.

Bytes 2-5 - UINT16 RR Intervals: 55, 4, 7, 3

You probably don't care about these, but there are two UINT16 values here (there can be an arbitrary number of RR-Interval values). BLE is always little-endian, so [55, 4] is 1,079 (55 + 4<<8), and [7, 3] is 775 (7 + 3<<8).

I believe the docs are a little confusing on this one. The XML suggests that these values are in seconds, but the comments say "Resolution of 1/1024 second." The normal way to express this would be <BinaryExponent>-10</BinaryExponent>, and I'm certain that's what they meant. So these would be:

  • RR0: 1.05s (1079/1024)
  • RR1: 0.76s (775/1024)


来源:https://stackoverflow.com/questions/65443033/heart-rate-value-in-ble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!