files/en-us/web/api/bluetooth/requestdevice/index.md
{{APIRef("Bluetooth API")}}{{SeeCompatTable}}{{securecontext_header}}
The Bluetooth.requestDevice() method of the {{domxref("Bluetooth")}} interface returns a {{jsxref("Promise")}} that fulfills with a {{domxref("BluetoothDevice")}} object matching the specified options.
If there is no chooser UI, this method returns the first device matching the criteria.
requestDevice()
requestDevice(options)
options {{optional_inline}}
filters {{optional_inline}}
: An array of filter objects indicating the properties of devices that will be matched.
To match a filter object, a device must match all the values of the filter: all its specified services, name, namePrefix, and so on.
Each filter consists of an array of objects with the following properties:
services {{optional_inline}}
'battery_service' or 'blood_pressure'.
You can also pass a full service UUID such as '0000180F-0000-1000-8000-00805f9b34fb' or the short 16-bit (0x180F) or 32-bit alias.
Note that these are the same values that can be passed to {{domxref("BluetoothUUID/getService_static","BluetoothUUID.getService()")}}.name {{optional_inline}}
namePrefix {{optional_inline}}
manufacturerData {{optional_inline}}
companyIdentifier
0x000C, you would specify 12.dataPrefix {{optional_inline}}
mask {{optional_inline}}
dataPrefix.serviceData {{optional_inline}} <!-- BluetoothServiceDataFilterInit -->
service
services array.dataPrefix {{optional_inline}}
mask {{optional_inline}}
dataPrefix.exclusionFilters {{optional_inline}}
filters.optionalServices {{optional_inline}}
: An array of optional service identifiers.
The identifiers take the same values as the elements of the services array (a GATT service name, service UUID, or UUID short 16-bit or 32-bit form).
optionalManufacturerData {{optional_inline}}
: An optional array of integer manufacturer codes.
This takes the same values as companyIdentifier.
The data is not used for filtering the devices, but advertisements that match the specified set are still delivered in advertisementreceived events.
This is useful because it allows code to specify an interest in data received from Bluetooth devices without constraining the filter controlling which devices are presented to the user in the permission prompt.
acceptAllDevices {{optional_inline}}
: A boolean value indicating that the requesting script can accept all Bluetooth devices.
The default is false.
This option is appropriate when devices have not advertised enough information for filtering to be useful.
When acceptAllDevices is set to true you should omit all filters and exclusionFilters, and you must set optionalServices to be able to use the returned device.
After the user selects a device to pair in the current origin, it is only allowed to access services whose UUID was listed in the services list in any element of filters.services or in optionalServices.
It is therefore important to list the required services.
In particular, when filtering with just name you must remember to also specify the desired services in optionalServices.
[!NOTE] Even though the
optionsargument is technically optional, in order to return any results you must either set a value forfiltersor setacceptAllDevicestotrue.
A {{jsxref("Promise")}} to a {{domxref("BluetoothDevice")}} object.
options do not make sense.
For example, if options.filters is present and options.acceptAllDevices is true, options.filters is not present and options.acceptAllDevices is false, or options.filters is [].NotFoundError {{domxref("DOMException")}}
SecurityError {{domxref("DOMException")}}
// 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((device) => {
console.log(`Name: ${device.name}`);
// Do something with the device.
})
.catch((error) => console.error(`Something went wrong. ${error}`));
Detailed examples are in the specification and also in Communicating with Bluetooth devices over JavaScript on developer.chrome.com.
{{Specifications}}
{{Compat}}