Web Bluetooth: Want to exclude certain devices when using navigator.bluetooth.requestDevice

377 Views Asked by At

I have an array with the names of some devices I don't want to get when I do navigator.bluetooth.requestDevice

Is there a way to keep them out the same way you can filter devices by name?

navigator.bluetooth.requestDevice({ filters: [ { namePrefix: "SPDCM", }, ], optionalServices: [UUID_FOTA_SERVICE] })

2

There are 2 best solutions below

0
Vincent Scheib On

There is not a way to exclude some devices as of March 2023.
The requestDevice filters (MDN docs & specification examples) only select criteria to include, but not mask, items to be included.

I've started a specification issue requesting exclusion filters. Please review and comment there to make a proposal. Adding as many specifics about your use case (e.g. specific device examples) will help motivate the work.

0
François Beaufort On

Chrome 114 has the new "exclusionFilters" option in navigator.bluetooth.requestDevice() that allows web developers to exclude some devices from the browser picker. It can be used to exclude devices that match a broader filter but are unsupported.

// Request access to a bluetooth device whose name starts with "Created by".
// The device named "Created by Francois" has been reported as unsupported.
const device = await navigator.bluetooth.requestDevice({
  filters: [{ namePrefix: "Created by" }],
  exclusionFilters: [{ name: "Created by Francois" }],
});

You can play with it at https://googlechrome.github.io/samples/web-bluetooth/exclusion-filters.html

Resources: