• It seems you're doing:

    try {
     ...
    } catch (e) {
      ..
    }
    

    but what you need to do to catch promises (like the one in setLight) is:

    ....then(function() {
      // .....
    }).catch(function(error) {
       // here
    });
    

    So not the usual try...catch - it's a bit weird, but it's just the way you have to handle stuff with JS promises.

    While you can catch other errors, unfortunately that NRF Error won't be catchable - it's right from the lower levels of the hardware.

    To me, it looks like you're not doing any checks to see whether you're in the middle of doing something with Bluetooth - so quite likely what'll happen is it'll turn on the light, get too bright, and try and turn itself off - which could happen while Puck.js is trying to disconnect, which could really mess things up?

    Could you try replacing setLight with this?

    var isBusy = false;
    function setLight(isOn) {
      var gatt;
      if (isBusy) throw new Error("Busy!");
      isBusy = true;
      NRF.connect("98:7b:f3:67:75:33").then(fu­nction(g) {
        gatt = g;
        return gatt.getPrimaryService("33160fb9-5b27-4e­70-b0f8-ff411e3ae078");
      }).then(function(service) {
        return service.getCharacteristic("217887f8-0af2­-4002-9c05-24c9ecf71600");
      }).then(function(characteristic) {
        characteristic.writeValue(isOn);
      }).then(function(){
        gatt.disconnect();
        console.log("Done!");
        isBusy = false;
      }).catch(function(e) {
        console.log("Problem: "+e);
        if (gatt) gatt.disconnect();
        isBusy = false;
      });
    }
    
    

    It might help, as it'll refuse to do anything else if it's already in the middle of controlling the light.

About

Avatar for Gordon @Gordon started