覆盖navigator.bluetooth.requestDevice()的默认行为

时间:2019-01-10 15:50:48

标签: google-chrome bluetooth web-bluetooth

当我致电navigator.bluetooth.requestDevice({acceptAllDevices:true})时,会弹出一个chrome窗口,其中包含我周围的设备。我只能在这里选择1台设备。是否可以选择多个设备或不弹出此窗口?我可以实现自己的基于Web的窗口来显示我周围的BLE设备吗?

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

enter image description here

2 个答案:

答案 0 :(得分:0)

Web Bluetooth GATT Communication API不允许您跳过此提示。参见https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#request_bluetooth_devices

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

它尚未在Chrome中完全实现。 按照https://github.com/WebBluetoothCG/web-bluetooth/blob/master/implementation-status.md跟踪更改。

答案 1 :(得分:0)

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

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

它包含开发人员指南的链接。

最简单的骇客用途是:

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)