I’m tasked with writing a configuration tool for a BLE HID device. The tool will ideally run on iOS/Android/macOS/Win/ChromOS so our initial thought was a web app using the Web Bluetooth API. There is a fundamental problem we can’t figure out how to work around.
If our device is paired with the OS and our application is launched it needs to discover the device and then control it using a custom service. Calling requestDevices() lets us discover and control UNPAIRED devices that are advertising, but doesn’t enumerate the connected devices. getDevices() enumerates the connected devices but subsequent calls to access services fail.
So, Is what we want to do possible with Web Bluetooth or any other cross-platform approach(electron, flutter, etc)?
// Connect to the device
try {
console.log('Getting existing permitted Bluetooth devices...');
const connectedDevices = await navigator.bluetooth.getDevices();
console.log('Got ' + connectedDevices.length + ' Bluetooth devices.');
for (const possibleDevice of connectedDevices) {
try {
const server = possibleDevice.gatt;
console.log('Device: ' + possibleDevice.name + ' (' + possibleDevice.id + ') - Connected: ' + server.connected);
if (!server.connected) {
try {
await server.connect();
}
catch (error) {
console.error('Error during reconnection process:', error);
}
}
console.log('Getting Device Information Service...');
const service = await server.getPrimaryService('device_information');
const model = await service.getCharacteristic(model_number_string);
await model.readValue().then(value => {
console.log('Model: ' + decoder.decode(value));
if ((decoder.decode(value)).includes('Cato')) {
console.log('Found Cato');
setDevice(possibleDevice);
}
})
if (possibleDevice === device)
break
} catch (error) {
console.error('Error during connection process:', error);
}
}
Connect to unpaired device works:
BluetoothConnector.js:66 Looking for devices...
20:37:00.163 BluetoothConnector.js:72 Device selected: BluetoothDevice {
id: 'z+Z+osCNpR6WsoaF3iJStQ==', name: "spero's cato (1)",
gatt: BluetoothRemoteGATTServer, ...
20:37:01.222 BluetoothConnector.js:75 Connected to GATT server.
20:37:01.756 BluetoothConnector.js:78 Motion service obtained:
20:37:01.756 BluetoothConnector.js:81 Enable characteristic obtained:
Subsequently trying to reconnect after browser refresh fails
Navigated to http://localhost:3000/dashboard
20:37:28.145 BluetoothConnector.js:22 Getting existing permitted Bluetooth .
20:37:28.147 BluetoothConnector.js:24 Got 4 Bluetooth devices.
BluetoothConnector.js:28 Device: spero's cato (1) (z+Z+osCNpR6WsoaF3iJStQ==) - Connected: false
BluetoothConnector.js:34 Error during reconnection process: NetworkError: Bluetooth Device is no longer in range.
BluetoothConnector.js:38 Getting Device Information Service...
20:37:28.152 BluetoothConnector.js:52 Error during connection process: NetworkError: Failed to execute 'getPrimaryService' on 'BluetoothRemoteGATTServer': GATT Server is disconnected. Cannot retrieve services. (Re)connect first with device.gatt.connect`.
sperok is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.