0

我关注用于 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 的连接持久性?

4

1 回答 1

0

你的代码对我来说看起来不错。也可以在https://googlechrome.github.io/samples/web-bluetooth/link-loss.html找到类似的代码

但是,我想知道您要写入哪个特征。如果不支持,Android 设备可能会简单地断开连接。

查看https://www.chromium.org/developers/how-tos/file-web-bluetooth-bugs#TOC-Android并获取 Android adb 日志(如果有帮助)。

于 2018-03-02T07:32:14.623 回答