Flutter ble读取体重秤特征值

Flutter ble read weight scale characteristic value

我是 flutter 的新手,我正在尝试连接到 A&D ble 体重秤并从服务 00002a9d-0000-1000-8000-00805f9b34fb 获取体重数据,我得到值 [1, 48, 22] 我正在使用 flutter_blue 库,重量值为 56.8 磅,如何将其转换为数组值到实际重量 value.TIA

权重服务0x2A98的documentation向您展示了服务的不同领域和特征。

Included in the characteristic value are a Flags field (for showing the presence of optional fields and measurement units), a Weight field, and depending upon the contents of the Flags field, may include one or more optional fields defined in 2.

第 3.2 章描述了字段:

  1. 标志字段
  2. 权重字段
  3. 时间戳字段(可选)
  4. 用户 ID 字段(如果设置了标志则包含)
  5. BMI 和身高字段对(如果设置了标志则包括在内)

根据这些信息,我们可以假设您收到的 3 个字节是标志字段和权重字段。

标志字段设置为 1 或十六进制的 0x01,这意味着仅设置了第一位。根据文档,第一位描述了测量单位。值为 1 表示测量单位设置为英制。

您收到的第二个和第三个字节包含分为两个字节的 uint16 形式的权重。我们必须弄清楚值的字节顺序(字节顺序)。我们可以使用 hex to integer converter:

您收到的两个值(48 和 22)可以用十六进制表示为 0x300x16。根据哪个值先出现,如果我们将它们合并在一起,我们会得到不同的值:

0x3016 -> 12310
0x1630 -> 5680

由于您测量的重量是 56.8 磅,我们现在知道订单 0x1630 代表小数点后两位数字的正确值。

您可以像这样从列表中提取正确的值:

void main() {
  List<int> received = [1,48,22];
  // Get the weight value from the list starting at index 1
  print(getWeight(received, 1));
}

double getWeight(List<int> data, index) {
  return (( 0xff & data[index + 1] ) << 8 | ( 0xff & data[index] ) << 0 ) / 100;
}