Bluetooth: requestDevice() method
The Bluetooth.requestDevice() method of the Bluetooth interface returns a Promise to a BluetoothDevice object with the specified options. If there is no chooser UI, this method returns the first device matching the criteria.
Syntax
requestDevice()
requestDevice(options)
Parameters
options Optional
-
An object that sets options for the device request. The available options are:
-
filters[]
-
An array of BluetoothScanFilters. This filter consists of an array of BluetoothServiceUUIDs, a name parameter, and a namePrefix parameter.
-
optionalServices[]
-
An array of BluetoothServiceUUIDs.
-
acceptAllDevices
-
A boolean value indicating that the requesting script can accept all Bluetooth devices. The default is false.
Return value
Exceptions
-
TypeError
-
Thrown if the provided options do not make sense. For example, options.filters is present and options.acceptAllDevices is true, or if options.filters is not present and options.acceptAllDevices is false. Or options.filters is [].
NotFoundError DOMException
-
Thrown if there is no Bluetooth device that matches the specified options.
SecurityError DOMException
-
Thrown if this operation is not permitted in this context due to security concerns. For example, it is called from insecure origin.
Examples
let options = {
filters: [
{ services: ["heart_rate"] },
{ services: [0x1802, 0x1803] },
{ services: ["c48e6067-5295-48d3-8d5c-0395f61792b1"] },
{ name: "ExampleName" },
{ namePrefix: "Prefix" },
],
optionalServices: ["battery_service"],
};
navigator.bluetooth
.requestDevice(options)
.then((device) => {
console.log(`Name: ${device.name}`);
})
.catch((error) => console.error(`Something went wrong. ${error}`));
Detailed examples are in the specification.
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
requestDevice |
56 |
79 |
No |
No |
43 |
No |
No |
56 |
No |
43 |
No |
6.0 |