Reputation: 113
I use web bluetooth in my project, I connect to the device using this code
async function requestDevice() {
bluetoothDevice = await navigator.bluetooth.requestDevice({
// filters: [ { name: 'Device test' } ],
acceptAllDevices: true,
optionalServices: [
'battery_service',
'03b80e5a-ede8-4b33-a751-6ce34ec4c700'
]
});
bluetoothDevice.addEventListener('gattserverdisconnected', onDisconnected);
}
Always to connect, I call the dialog box and there I select the device I need. Can I programmatically get a list of already connected devices? In order not to invoke a dialog box each time?
Upvotes: 1
Views: 2478
Reputation: 566
There is not currently a way to get a list of Bluetooth devices in Chrome. However, I am working on implementing a getDevices()
method for Bluetooth. Please follow the status in Chrome Platform Status
6/22/20 Edit:
I recently implemented a new permissions backend as well as two APIs that will enable previously permitted Bluetooth devices to be used.
The new permissions backend is implemented behind the chrome://flags/#enable-web-bluetooth-new-permissions-backend. The new backend will persist device permissions granted through requestDevice()
until the permission is reset in Site Settings or the Page Info dialog box.
The getDevices()
and watchAdvertisements()
are implemented behind the chrome://flags/#enable-experimental-web-platform-features flag for Chrome 85.0.4165.0 or greater. The recommended use of these APIs is to use getDevices()
to retrieve an array of permitted BluetoothDevices and then calling watchAdvertisements()
on these devices to start a scan. When advertisement packets are detected from the devices, the advertisementreceived
Event will be fired on the device that it corresponds to. At this point, the Bluetooth device is in range and can be connected to.
Please give this new feature a try, and file any bugs at https://crbug.com using the Blink>Bluetooth component.
Upvotes: 7