Search code examples
electronbluetooth-lowenergyweb-bluetooth

Electron Web Bluetooth API requestDevice() Error


I'm trying to develop an application which communicates with Bluetooth Low Energy Devices. I established a working "website" with the Web Bluetooth API. Everything works fine, so I used the Electron framework, to build an application.

The issue is known - if you start navigator.bluetooth.requestDevice(), you get this error message: User cancelled the requestDevice() chooser..

This causes due to the missing device chooser in Chromium. There are several topics about workarounds I found, but no examples. This is my first Electron project. Maybe somebody solved this problem and can give me a hint :-)


Solution

  • Here is a code sample that will just return the first device instead of having to implement a device chooser:

      mainWindow.webContents.on('select-bluetooth-device', (event, deviceList, callback) => {
        event.preventDefault();
        console.log('Device list:', deviceList);
        let result = deviceList[0];
        if (!result) {
          callback('');
        } else {
          callback(result.deviceId);
        }
      });
    

    Source: https://electronjs.org/docs/api/web-contents#event-select-bluetooth-device