Web BLE 特性 startNotifications 有时不绑定
Web BLE Characteristic startNotifications sometimes doesn't bind
我正在使用网络 BLE。我根据心率测量的例子编写了我的代码。
大部分时间一切正常。但有时,即使连接成功,当我尝试绑定到通知时,它也不起作用。
link是在这个函数里做的:
_startNotifications(characteristicUuid) {
let characteristic = this._characteristics.get(characteristicUuid);
console.log(characteristic);
return characteristic.startNotifications().then(() => characteristic);
}
一切正常后,我可以在控制台中看到 BluetoothRemoteGATTCharacteristic
有一个 value : DataView(2) {}
否则,当它不工作时它有一个 value : null
如果我检测到该值为空,我希望能够自动重试。但我不熟悉 Promise(我认为就是它)并且 console.log(characteristic.value)
在这里不起作用。
你会如何处理这个问题?
我最终做的是 "bypass" 问题。所以这是一个比纯 Javascript 更算法的分辨率。
连接函数我没有改,所以还是这样调用:
device._startNotifications(some_uuid).then(handleHeartRateMeasurement)
我检查了 handleHeartRateMeasurement
函数中的所有内容:
var ready = false;
function handleHeartRateMeasurement(heartRateMeasurement) {
console.log("Hold on...");
heartRateMeasurement.addEventListener("characteristicvaluechanged", event => {
// Everytime the value change, this should be triggered
// If it did not, variable "ready" will stay false
ready = true;
var value = device.parseValue(event.target.value);
// Do something with value here
});
var check = function(){
// If we have received data from characteristic, we are ready to go !
if(ready === false){
console.log("Device connected but not receiving data");
// Stop the current notification subscription
device.stopNotificationsHeartRateMeasurement();
// Start a new one
device._startNotifications(some_uuid).then(handleHeartRateMeasurement);
setTimeout(check, 1000); // check again in a 1s
}
else{
console.log("Device connected and receiving data");
}
}
setTimeout(() => {
check();
}, 500);
}
我正在使用网络 BLE。我根据心率测量的例子编写了我的代码。
大部分时间一切正常。但有时,即使连接成功,当我尝试绑定到通知时,它也不起作用。
link是在这个函数里做的:
_startNotifications(characteristicUuid) {
let characteristic = this._characteristics.get(characteristicUuid);
console.log(characteristic);
return characteristic.startNotifications().then(() => characteristic);
}
一切正常后,我可以在控制台中看到 BluetoothRemoteGATTCharacteristic
有一个 value : DataView(2) {}
否则,当它不工作时它有一个 value : null
如果我检测到该值为空,我希望能够自动重试。但我不熟悉 Promise(我认为就是它)并且 console.log(characteristic.value)
在这里不起作用。
你会如何处理这个问题?
我最终做的是 "bypass" 问题。所以这是一个比纯 Javascript 更算法的分辨率。
连接函数我没有改,所以还是这样调用:
device._startNotifications(some_uuid).then(handleHeartRateMeasurement)
我检查了 handleHeartRateMeasurement
函数中的所有内容:
var ready = false;
function handleHeartRateMeasurement(heartRateMeasurement) {
console.log("Hold on...");
heartRateMeasurement.addEventListener("characteristicvaluechanged", event => {
// Everytime the value change, this should be triggered
// If it did not, variable "ready" will stay false
ready = true;
var value = device.parseValue(event.target.value);
// Do something with value here
});
var check = function(){
// If we have received data from characteristic, we are ready to go !
if(ready === false){
console.log("Device connected but not receiving data");
// Stop the current notification subscription
device.stopNotificationsHeartRateMeasurement();
// Start a new one
device._startNotifications(some_uuid).then(handleHeartRateMeasurement);
setTimeout(check, 1000); // check again in a 1s
}
else{
console.log("Device connected and receiving data");
}
}
setTimeout(() => {
check();
}, 500);
}