Web蓝牙特性.writeValue自动断开连接

问题描述 投票:0回答:1

我关注Web / Bluetooth的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个回答
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日志,如果有帮助的话。

© www.soinside.com 2019 - 2024. All rights reserved.