Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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
Bluetooth.requestDevice([options]) .then(function(bluetoothDevice) { ... })
Returns
A Promise
to a BluetoothDevice
object.
Parameters
- options Optional
- An object that sets options for the device request. The available options are:
filters[]
: An array ofBluetoothScanFilters
. This filter consists of an array ofBluetoothServiceUUID
s, aname
parameter, and anamePrefix
parameter.optionalServices[]
: An array ofBluetoothServiceUUID
s.acceptAllDevices
: ABoolean
indicating that the requesting script can accept all Bluetooth devices. The default isfalse
.
Exceptions
TypeError
- The provided
options
do not makes sense. For example,options.filters
is present andoptions.acceptAllDevices
istrue
, or ifoptions.filters
is not present andoptions.acceptAllDevices
isfalse
. Oroptions.filters
is[]
. NotFoundError
- There is no Bluetooth device that matches the specified options.
SecurityError
- This operation is not permitted in this context due to security concerns. For example, it is called from insecure origin.
Example
// Discovery options match any devices advertising: // . The standard heart rate service. // . Both 16-bit service IDs 0x1802 and 0x1803. // . A proprietary 128-bit UUID service c48e6067-5295-48d3-8d5c-0395f61792b1. // . Devices with name "ExampleName". // . Devices with name starting with "Prefix". // // And enables access to the battery service if devices // include it, even if devices do not advertise that service. 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(function(device) { console.log('Name: ' + device.name); // Do something with the device. }) .catch(function(error) { console.log("Something went wrong. " + error); });
Detailed examples are in the specification.
Specifications
Specification | Status | Comment |
---|---|---|
Web Bluetooth The definition of 'requestDevice()' in that specification. |
Draft | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
requestDevice | Chrome
Full support
56
| Edge
Full support
≤79
| Firefox No support No | IE No support No | Opera
Full support
43
| Safari No support No | WebView Android No support No | Chrome Android Full support 56 | Firefox Android No support No | Opera Android Full support 43 | Safari iOS No support No | Samsung Internet Android Full support 6.0 |
Legend
- Full support
- Full support
- No support
- No support
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.