【问题标题】:Override default behavior of navigator.bluetooth.requestDevice()覆盖 navigator.bluetooth.requestDevice() 的默认行为
【发布时间】:2020-08-17 23:55:12
【问题描述】:

当我调用 navigator.bluetooth.requestDevice({acceptAllDevices: true}) 时,会弹出一个带有我周围设备的 chrome 窗口。我只能在这里选择 1 个设备。有没有办法选择多个设备或不弹出此窗口;我可以实现自己的基于 Web 的窗口来显示我周围的 BLE 设备吗?

navigator.bluetooth.requestDevice({acceptAllDevices: true})
         .then(device => {
              console.log(device);
         });

【问题讨论】:

    标签: google-chrome bluetooth web-bluetooth


    【解决方案1】:

    Web 蓝牙 GATT 通信 API 不允许您绕过此提示。见https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#request_bluetooth_devices

    不过,即将推出的网络蓝牙扫描 API 可让您扫描附近的广告并连接到设备:https://webbluetoothcg.github.io/web-bluetooth/scanning.html

    它尚未在 Chrome 中完全实现。 关注https://github.com/WebBluetoothCG/web-bluetooth/blob/master/implementation-status.md 跟踪变化。

    【讨论】:

    • 可以同时连接2台设备吗?
    • 您可以连接 2 台设备。一个接一个是的。
    • @FrançoisBeaufort 我们什么时候可以期待 requestLeScan API?
    • 没有预计到达时间。我建议你在bugs.chromium.org/p/chromium/issues/detail?id=897312 上给铬虫加星标。
    【解决方案2】:

    新的navigator.bluetooth.getDevices API(在 Chrome 85 及更高版本中)实际上允许您避免出现此提示 IF 您之前曾使用 requestDevice 配对设备。

    上面的 chromestatus 页面在这里:https://www.chromestatus.com/feature/4797798639730688

    它包含一个开发者指南的链接。

    最简单的hackiest用法是:

    navigator.bluetooth.getDevices().then(function(devices) {
      if (devices.length==0) put_up_button_for_requestDevice();
      else return devices[0].gatt.connect();
    }).then(finish_connecting_as_normal)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-10
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多