• Hi.
    I've created Android app that serves Location and Navigation service, Device info, and Time service (standard GATT profiles). LNS has 3 characteristics.
    Currently in Bangle JS2 client app I can subscribe to one service, one characteristic.

    function connection_setup() {
      NRF.requestDevice({filters:[{namePrefix:"Pixel 4a"}]}).then(function(d) {
        device = d;
        console.log("Found device: "+d.name);
        E.showMessage("Found device"+d.name);
        return device.gatt.connect();
      }).then(function(ga) {
        gatt = ga;
        E.showMessage("Connected");
        console.log("Connected");
        lastReceivedTime = new Date();
        return gatt.getPrimaryService("1819"); //How to get another services here?
      }).then(function(s) {
        service = s;
        return service.getCharacteristic("2a67"); //How to get other characteristics here?
      }).then(function(c) {
        characteristic = c;
        characteristic.on('characteristicvaluechanged', (event)=>updateSpeed(event));
        return characteristic.startNotifications();
      }).then(function() {
        console.log("Notifications subsribed");
        E.showMessage("Notifications subsribed");
      }).catch(function(e) {
        E.showMessage(e.toString(), "ERROR");
        console.log(e);
      });
    }
    

    How to subscribe for LNS multiple characteristics, and to Time service, and also query Device info? Each .then() has only one return, I am not getting how to fork or chain it.

  • it looks like async/await is not supported, is it?

  • well, for chaining you can simply continue in same way, before line 23 you can continue like line 12 with another service?

  • Hmm... well, yes, why not.... I'll try.

  • I'm in a bit of similar situation, did you get it working? Able to share the code?
    Trying myself but as I'm not understanding everything, it's a hit-or-miss if I succeed.

  • Slight progress as I was able to chain two characteristics, but only the second one showing co2 values is doing something. The first one is supposed to show temperature, but seems it is not reached as I only see co2 values. I've read up on JS promises but haven't found out how to receive and show several values.

      NRF.connect("F5:CB:47:42:E3:BB public").then(function(g) {
        gatt = g;
    
        return gatt.getPrimaryService("07e1f0e2-b68f-4f71-8bfc-19b3b0427b68");
      }).then(function(service) {
        return service.getCharacteristic("5d75d722-fce9-471b-93e2-6c625ef6d634");
      }).then(function(characteristic) {
        characteristic.on('characteristicvaluechanged', function(temperature) {
    
          temp = temperature.target.value.buffer;
          temp = (temp[1] * 256 + temp[0]) / 100;
          console.log("Temp: ", temp);
        });
        //return characteristic.startNotifications();
        return gatt.getPrimaryService("07e1f0e2-b68f-4f71-8bfc-19b3b0427b68");
    
      }).then(function(service) {
        return service.getCharacteristic("5d75d723-fce9-471b-93e2-6c625ef6d634");
      }).then(function(characteristic) {
         characteristic.on('characteristicvaluechanged', function(co2) {
     
          co2 = co2.target.value.buffer;
          co2 = (co2[1] * 256 + co2[0]);
          console.log("co2 : ", co2);
      });
          return characteristic.startNotifications();
      }).then(function() {
        console.log("Done!");
      }).catch(function(e) {
        E.showMessage(e.toString(), "ERROR");
        console.log(e);
      });
    
  • The first one is supposed to show temperature, but seems it is not reached as I only see co2 values.

    maybe because you commented out line 14 ?

    //return characteristic.startNotifications();
    
  • Thx, but I've tried that as well. If I uncomment it, I get a message on next line Unreachable 'return' after 'return', and this time only the temperature is being shown, not the co2 value. So I assume a function can only return one "thing", not several.

  • oh, of course you need to add one more

      }).then(function() {
    

    chain between returns like lines 15-19

  • Yes, this works! It makes sense now when I added it and see the whole code.
    Thx a lot @fanoush !
    Posting the whole code here so others may benefit from it

      NRF.connect("F5:CB:47:42:E3:BB public").then(function(g) {
        gatt = g;
    
        return gatt.getPrimaryService("07e1f0e2-b68f-4f71-8bfc-19b3b0427b68");
      }).then(function(service) {
        return service.getCharacteristic("5d75d722-fce9-471b-93e2-6c625ef6d634");
      }).then(function(characteristic) {
        characteristic.on('characteristicvaluechanged', function(temperature) {
          temp = temperature.target.value.buffer;
          temp = (temp[1] * 256 + temp[0]) / 100;
          console.log("Temp: ", temp);
        });
        return characteristic.startNotifications();
      }).then(function(){
        return gatt.getPrimaryService("07e1f0e2-b68f-4f71-8bfc-19b3b0427b68");
      }).then(function(service) {
        return service.getCharacteristic("5d75d723-fce9-471b-93e2-6c625ef6d634");
      }).then(function(characteristic) {
         characteristic.on('characteristicvaluechanged', function(co2) {
          co2 = co2.target.value.buffer;
          co2 = (co2[1] * 256 + co2[0]);
          console.log("co2 : ", co2);
      });
          return characteristic.startNotifications();
      }).then(function() {
        console.log("Done!");
      }).catch(function(e) {
        E.showMessage(e.toString(), "ERROR");
        console.log(e);
      });
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

BLE client. How to subscribe for multiple notifications?

Posted by Avatar for Mark_M @Mark_M

Actions