问题描述
我需要从bluetooth thermometer中读取温度。我不确定温度数据将如何发送到应用程序。我正在为蓝牙使用react-native-ble-plx软件包,但也不确定是否使用了正确的软件包。
由于我是第一个涉及蓝牙的项目,因此我对蓝牙通信的知识不多。我不知道我将从哪个特性获得温度。我已经尝试了所有特性,但是没有一个值与温度值匹配。我将在此处附上我的代码段。我已经搜索了各种论坛并获得了堆栈溢出的答案,但我仍然不了解这些特征的工作原理。
const subscription = this.manager.onStateChange((state) => {
if (state === 'PoweredOn') {
this.scanAndConnect();
subscription.remove();
}
},true);
scanAndConnect() {
this.manager.startDeviceScan(null,null,(error,device) => {
console.log(device)
if (error) {
console.log(error)
// Handle error (scanning will be stopped automatically)
return
}
if (device.name == 'CVTE') {
this.setState({ deviceData: device })
// Stop scanning as it's not necessary if you are scanning for one device.
this.manager.stopDeviceScan();
// Proceed with connection.
device.connect()
.then((device) => {
return device.discoverAllServicesAndcharacteristics()
})
.then((device) => {
this.getServicesAndcharacteristics(device);
return device.serviceData
})
.then((data) => {
console.log(data);
},(error) => {
console.log(error.message);
})
}
});
}
getServicesAndcharacteristics(device) {
let characteristics = null
let self = this;
device.services().then(services => {
services.forEach((service,i) => {
service.characteristics().then(c => {
c.forEach(charac => {
if (charac.isReadable && charac.isNotifiable) {
self.subscribetoDevice(device,charac.serviceUUID,charac.uuid)
}
});
})
})
})
}
subscribetoDevice = async (device,SERVICE_UUID,CHaraCTERISTIC_UUID) => {
device.monitorCharacteristicForService(SERVICE_UUID,CHaraCTERISTIC_UUID,(err,characteristic) => {
this.getValueFromCharacteristic(characteristic);
})
}
getValueFromCharacteristic = async (characteristic) => {
characteristic.read().then(characteristic => {
var buffer = Buffer.from(characteristic.value);
console.log(buffer.readUInt8(1,true));
});
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)