从 react-native-ble-plx 中的 GATT 特征值获取权重
get weight from GATT characteristics value in react-native-ble-plx
我正在开发一个 react-native 应用程序,它获取支持蓝牙的体重秤 (MI SCALE2) 的重量值。(我对蓝牙一无所知。)
//版本
"react-native": "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);
})
}
});
据我了解您的代码,您的体重秤使用蓝牙 Weight Scale Profile and Weight Scale Service。
您在相应特征中找到的数据需要按照Personal Health Devices Transcoding
中的描述进行解释
编辑:
您可以在此处找到有关数据结构的更多信息:
GATT Specification Supplement 5
示例:
特征([33,0,0,0]) => 0x00000011 => ...00 0010 0001 =>
value
description
1
Time Stamp Supported: True
0
Multiple Users Supported: False
0
BMI Supported: False
0100
Weight Measurement Resolution: Resolution of 0.05 kg or 0.1 lb
000
Height Measurement Resolution: Not specified
体重 = [34, 156, 74, 178, 7, 1, 7, 22, 33, 2]
=> 0x22 0x9c 0x4a 0xb2 0x07 0x01 0x07 0x16 0x21 0x02
第一个字节是标志字段 => 0x22 => 0010 0010
value
description
0
Measurement Units: SI
1
Time Stamp present: True
0
User ID present: False
0
BMI and Height present: False
0010
Reserved for Future Use
重量以千克为单位,分辨率为 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
我正在开发一个 react-native 应用程序,它获取支持蓝牙的体重秤 (MI SCALE2) 的重量值。(我对蓝牙一无所知。)
//版本
"react-native": "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);
})
}
});
据我了解您的代码,您的体重秤使用蓝牙 Weight Scale Profile and Weight Scale Service。 您在相应特征中找到的数据需要按照Personal Health Devices Transcoding
中的描述进行解释编辑: 您可以在此处找到有关数据结构的更多信息: GATT Specification Supplement 5
示例:
特征([33,0,0,0]) => 0x00000011 => ...00 0010 0001 =>
value | description |
---|---|
1 | Time Stamp Supported: True |
0 | Multiple Users Supported: False |
0 | BMI Supported: False |
0100 | Weight Measurement Resolution: Resolution of 0.05 kg or 0.1 lb |
000 | Height Measurement Resolution: Not specified |
体重 = [34, 156, 74, 178, 7, 1, 7, 22, 33, 2] => 0x22 0x9c 0x4a 0xb2 0x07 0x01 0x07 0x16 0x21 0x02
第一个字节是标志字段 => 0x22 => 0010 0010
value | description |
---|---|
0 | Measurement Units: SI |
1 | Time Stamp present: True |
0 | User ID present: False |
0 | BMI and Height present: False |
0010 | Reserved for Future Use |
重量以千克为单位,分辨率为 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