mardi 2 juin 2020

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

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.
     }



Aucun commentaire:

Enregistrer un commentaire