2

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

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

在此处输入图像描述

4

2 回答 2

0

新的navigator.bluetooth.getDevicesAPI(在 Chrome 85 及更高版本中)实际上允许您避免此提示,如果您以前用于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)
于 2020-08-17T15:55:12.807 回答
0

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跟踪更改。

于 2019-01-11T15:04:40.130 回答