0

我正在开发一个 react-native 应用程序,它可以获取支持蓝牙的秤(MI SCALE2)的重量值。(我对蓝牙一无所知。)


// 版本

“反应原生”:“0.66.1”,

“react-native-ble-plx”:“https://github.com/below/react-native-ble-plx”,


当我上秤时,我能够得到这些值。

# feature
{"data": [33, 0, 0, 0], "type": "Buffer"}

# Weight
{"data": [2, 156, 74, 178, 7, 1, 7, 22, 33, 1], "type": "Buffer"}
{"data": [2, 156, 74, 178, 7, 1, 7, 22, 33, 1], "type": "Buffer"}
{"data": [2, 156, 74, 178, 7, 1, 7, 22, 33, 1], "type": "Buffer"}
{"data": [2, 156, 74, 178, 7, 1, 7, 22, 33, 2], "type": "Buffer"}
{"data": [2, 156, 74, 178, 7, 1, 7, 22, 33, 2], "type": "Buffer"}
{"data": [34, 156, 74, 178, 7, 1, 7, 22, 33, 2], "type": "Buffer"}
{"data": [162, 156, 74, 178, 7, 1, 7, 22, 33, 6], "type": "Buffer"}

看了几个地方的Q&A,才知道需要把特征的值和权重数组的值结合起来。

我想知道如何从我的结果中获取重量值,例如“94.9kg,95.5kg,...”

下面是我写的代码。


manager.startDeviceScan(null, null, (error, device) => {
      if (error) {
        console.log('error : ' + error);
        return;
      }

      console.log(device.name);

      if (device.name === 'MI SCALE2') {
        console.log('detected!!');
        manager.stopDeviceScan();
        device
          .connect()
          .then(device => {
            return device.discoverAllServicesAndCharacteristics();
          })
          .then(device => {
            return device.services();
          })
          .then(services => {
            const result = services.filter(id => id.uuid.indexOf('181d') != -1); // 181d is Weight Scale -> org.bluetooth.service.weight_scale;
            return result[0].characteristics();
          })
          .then(characters => {
            const resultDateObject = characters.filter(
              data => data.uuid.indexOf('2a2b') != -1, // 2a2b is Current Time -> org.bluetooth.characteristic.current_time;
            );
            const resultWeightFeature = characters.filter(
              data => data.uuid.indexOf('2a9e') != -1, // 2a9e is Weight Scale Feature -> org.bluetooth.characteristic.weight_scale_feature
            );
            const resultWeight = characters.filter(
              data => data.uuid.indexOf('2a9d') != -1, // 2a9d is Weight Measurement -> org.bluetooth.characteristic.weight_measurement;
            );
            const resultPosition2D = characters.filter(
              data => data.uuid.indexOf('2a2f') != -1, // 2a2f is Position 2D -> org.bluetooth.characteristic.position_2d;
            );
            
            // const DeviceID = resultWeightFeature[0].deviceID;
            // const ServiceUUID = resultWeightFeature[0].serviceUUID;
            // const DateCharacterUUID = resultDateObject[0].uuid;
            // const WeightFeatureCharacterUUID = resultWeightFeature[0].uuid;
            // const WeightCharacterUUID = resultWeight[0].uuid;
            // const PositionCharacterUUID = resultPosition2D[0].uuid;

            resultWeight[0].monitor((error, characteristic) => {
              if (error) {
                console.log('error:::::', error);
                return;
              }
              let your_bytes = Buffer.from(characteristic.value, "base64");
              console.log(your_bytes);
            })
            return resultWeightFeature[0].read();

          }).then(feature => {
            let feature_bytes = Buffer.from(feature.value, "base64");
            console.log('feature.value');
            console.log(feature_bytes);
          })
      }
    });

4

1 回答 1

1

据我了解您的代码,您的体重秤使用蓝牙体重秤配置文件体重秤服务。您在相应特征中找到的数据需要按照个人健康设备转码中的说明进行解释

编辑:您可以在此处找到有关数据结构的更多信息: GATT 规范补充 5

例子:

特征([33,0,0,0])=> 0x00000011 => ...00 0010 0001 =>

价值 描述
1 支持时间戳:真
0 支持多个用户:假
0 支持的 BMI:错误
0100 重量测量分辨率:分辨率为 0.05 kg 或 0.1 lb
000 高度测量分辨率:未指定

权重 = [34, 156, 74, 178, 7, 1, 7, 22, 33, 2] => 0x22 0x9c 0x4a 0xb2 0x07 0x01 0x07 0x16 0x21 0x02

第一个字节是标志字段 => 0x22 => 0010 0010

价值 描述
0 计量单位:SI
1 时间戳存在:真
0 存在用户 ID:假
0 存在 BMI 和身高:错误
0010 保留供将来使用

重量以千克为单位,分辨率为 0.005 (uint16) => 0x4a9c => 95,5 kg

时间戳 0xb2 0x07 0x01 0x07 0x16 0x21 0x02

年(uint16)=> 0x07b2 => 1970

月(uint8)=> 0x01 => 1

天(uint8)=> 0x07 => 7

小时(uint8)=> 0x16 => 22

分钟(uint8)=> 0x21 => 33

秒(uint8)=> 0x02 => 2

日期 1970-01-07T22:33:02

于 2022-02-02T11:06:18.903 回答