Web Bluetooth API Connecting multiple devices in a single web page with same events priority

Viewed 621

I would connect multiple hardware devices with BLE to a single HTML/javascript web page. I have used Web Bluetooth API. Everything is working fine. I'm able to connect all devices and read out single characteristics from each one of those. My problem is that I would have the same priority for each device. It seems that last connected device got higher priorities than previous ones in terms of events polling out. Is there a way for attributing the same priority to all the devices?

Here is my code for one device. All devices offer the same service and same characteristic.

 //for device 1
    function connect_device_1(){
       navigator.bluetooth.requestDevice({
       filters: [
       {services: myService},
       {name: myDeviceName},    
       ]       
    })
    .then(function(device) {
       return device.gatt.connect();
    })
    .then(function(server) {
       return server.getPrimaryService(myService);  
    })
    .then(function(service) {
       return service.getCharacteristics();
    })
    .then(function(characteristics) {
       sNots(characteristics);
    })
    .catch(function(error) {    
    console.error('Connection failed!', error);
    });  
    }

    async function sNots(characteristics) {
        try {
          characteristics[0].addEventListener('characteristicvaluechanged', handleData1);
          await  characteristics[0].startNotifications(); 
        } catch (e) {
          console.log("Characteristic error!");
        }
     }

     function handleData1(event) {
             //only one read out for cycle
     }

     //same as above for device 2
     //....
     function handleData2(event) {

     }
     //same as above for device 3
     //....
     function handleData3(event) {

     }
     //same as above for device 4
     //....
     function handleData4(event) {
          //For instance, if the device 4 is connected as the last one, this event is called more 
          //than all the others, while I would like to have same priority with all the others.
     }
1 Answers

If your devices stream a lot of notifications, then this is the expected behaviour how BLE connection scheduling is usually implemented inside the Bluetooth controller, when you have the same connection interval among all connections.

See Bluetooth 5 & BLE throughput over multiple connections and https://infocenter.nordicsemi.com/topic/sds_s132/SDS/s1xx/multilink_scheduling/central_connection_timing.html.

There is not much you can do about this. You could try, from the peripheral side to select different connection parameters to avoid this situation. Normally, you can hint the controller how long connection events you want when you initiate a connection over HCI. But neither Web Bluetooth nor the underlying Bluetooth stacks Web Bluetooth uses expose this setting, leaving it to 0 (unspecified).

Related