Override default behavior of navigator.bluetooth.requestDevice()

Viewed 2507

When I call navigator.bluetooth.requestDevice({acceptAllDevices: true}), a chrome window pops up with the devices around me. I can only pick 1 device here. Is there a way to pick multiple devices or not have this window pop-up; Can I implement my own web based window that show BLE devices around me?

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

enter image description here

2 Answers

The Web Bluetooth GATT Communication API does not allow you to bypass this prompt. See https://developers.google.com/web/updates/2015/07/interact-with-ble-devices-on-the-web#request_bluetooth_devices

The upcoming Web Bluetooth Scanning API however will let you scan for nearby advertisements and connect to devices: https://webbluetoothcg.github.io/web-bluetooth/scanning.html

It is not fully implemented yet in Chrome. Follow https://github.com/WebBluetoothCG/web-bluetooth/blob/master/implementation-status.md to keep track of changes.

The new navigator.bluetooth.getDevices API (in Chrome 85 and later) actually allows you to avoid this prompt IF you've previously used requestDevice to pair a device.

The chromestatus page on it is here: https://www.chromestatus.com/feature/4797798639730688

And it contains a link to a developer guide.

The simplest hackiest use would be:

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)
Related