我关注用于 Web 蓝牙的 Google/chrome 示例。我有两个 writeValue 操作。一个是在 requestDevice 承诺中,它工作得很好。第二个,我在动作触发时保存了特征引用和writeValue。消息已发送,但连接自动中断。我正在使用 Mac OSX 10.13.3 和 chrome64 并修改 Android BLE 外设模拟器(来自 google github)
代码段---
var bluetoothDevice;
var myCharacteristic;
navigator.bluetooth.requestDevice({
...
})
.then(device => {
bluetoothDevice = device;
....
})
....
.then(characteristic =>{
myCharacteristic = characteristic;
let encoder = new TextEncoder('utf-8');
let sendMsg = encoder.encode("hello");
/*
It works...
*/
myCharacteristic.writeValue(sendMsg);
})
....
function onSendMessage() {
if (!bluetoothDevice.gatt.connected) {
log("device is disconnected")
return
}
if (!myCharacteristic){
log("no characteristic defined")
return
}
let encoder = new TextEncoder('utf-8');
let sendMsg = encoder.encode("hello");
/*
message sent but auto disconnect the peripheral
*/
myCharacteristic.writeValue(sendMsg);
}
有没有人有相同的经验和任何建议保持 writeValue 的连接持久性?