【问题标题】:Web Bluetooth characteristic.writeValue break the connection automaticallyWeb 蓝牙特性.writeValue 自动断开连接
【发布时间】:2018-03-02 15:32:14
【问题描述】:

我关注用于 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 保持连接持久性,是否有人有相同的经验和任何建议?

【问题讨论】:

    标签: android google-chrome web-bluetooth


    【解决方案1】:

    您的代码对我来说看起来不错。在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 日志(如果有帮助)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-09
      • 2018-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-18
      • 2016-01-30
      • 1970-01-01
      相关资源
      最近更新 更多