BLE 中的心率值

Heart Rate Value in BLE

我很难从 HR 特征中获得有效值。我显然没有在 Dart 中正确处理这些值。

示例数据:

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

标志字段: 我将主字节数组中的第一项转换为二进制以获取标志

22 = 10110 (as binary)

这让我相信它是 U16(位[0] == 1)

心率值:

因为它是 16 位的,所以我试图获取 1 和 2 索引中的字节。然后我尝试将它们缓冲到 ByteData 中。从那里我将它们转换为 Uint16,Endian 设置为 Little。这给了我 14136 的值。很明显,我遗漏了一些关于它应该如何工作的基本知识。

任何帮助澄清我不了解如何处理 16 位 BLE 值的内容将不胜感激。

谢谢。

  /*
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);
    }
  }

您更新后的数据看起来好多了。以下是如何对其进行解码,以及您自己从头开始解决这个问题的过程。

确定格式

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。 (自从改版后,不知道怎么不搜索就能找到这个页面,好像没有链接了。)

字节 0 - 标志:22 (0001 0110)

位从 LSB (0) 到 MSB (7) 编号。

  • Bit 0 - 心率值格式:0 => UINT8 次/分钟
  • 位 1-2 - 传感器接触状态:11 => 支持和检测
  • 位 3 - 能量消耗状态:0 => 不存在
  • 位 4 - RR 间隔:1 => 存在一个或多个值

RR 间隔的含义在上面链接的 HRS 文档中有解释。听起来你只是想要心率值,所以我就不在这里赘述了。

字节 1 - UINT8 BPM:56

由于标志位 0 为 0,因此这是每分钟的节拍数。 56.

字节 2-5 - UINT16 RR 间隔:55、4、7、3

你可能不关心这些,但这里有两个 UINT16 值(可以有任意数量的 RR-Interval 值)。 BLE 总是小端,所以 [55, 4] 是 1,079 (55 + 4<<8),[7, 3] 是 775 (7 + 3<<8).

我认为文档在这方面有点混乱。 XML 表明这些值以秒为单位,但评论说“分辨率为 1/1024 秒”。正常的表达方式是 <BinaryExponent>-10</BinaryExponent>,我确信这就是他们的意思。所以这些将是:

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