Persistent Permissions Implementation for Chrome

Hi Web Bluetooth Community Group,

I recently implemented a new permissions backed that allows Bluetooth
device permissions to persist across browser sessions. In addition,
Bluetooth device permissions can be managed by the user in Chrome's Site
Settings page and the Page Info dialog. The new backend can be enabled with
the chrome://flags/#enable-web-bluetooth-new-permissions-backend. I also
implemented two Web Bluetooth APIs to use with the new permission backend.

The getDevices() API can be used to receive a list of permitted Bluetooth
devices. Once we have the list of permitted devices, watchAdvertisements()
can be called on the device objects to scan for advertisement packets from
these devices. When a packet is detected by the system, an
'advertisementreceived' event will be fired on the device object. At this
point, we know that the device is currently in range of the system and has
been discovered by the Bluetooth adapter. These two APIs can be enabled
with the chrome://flags/#enable-experimental-web-platform-features. The
following JS example demonstrates how to use these APIs to reconnect to
permitted devices:

// Retrieve permitted devices.
navigator.bluetooth.getDevices()
    .then(devices => {
      for (let device of devices) {
        let abortController = new AbortController();
        device.addEventListener('advertisementreceived', evt => {
          // An advertisement packet for the device has been received.
          // The device is near the system and can be connected to.
          // Stop watching advertisements to conserve battery life.
          abortController.abort();
          evt.device.gatt.connect()
              .then(gattServer => {
                // The has been connected to and can be used.
              });
        }, {once: true});

        // Begin a scan for advertisement packets from the device.
        device.watchAdvertisements({signal: abortController.signal});
      }
    });

These features can be used in Chrome version 85.0.4165 or later.

Please give them a try and file bugs/feedback to GitHub
<https://github.com/WebBluetoothCG/web-bluetooth/issues/new> for
specification or crbug <https://bugs.chromium.org/p/chromium/issues/entry>
for implementation.

Thank you,

Ovidio Ruiz-Henríquez

Received on Wednesday, 8 July 2020 16:47:00 UTC