• This is my code so far. I'm struggling to understand how to not lose context through the promises chain - the callback I register for characteristicvaluechanged has no idea which BLE device sent an update!

    function connect() {
      nrfbusy = true;
      NRF.requestDevice({ timeout:4000,
                          active:true,
                          filters: [{ namePrefix: NAME_PREFIX }] }).then(function(device) {
      console.log("Found");
      show(0b10);
      return device.gatt.connect({minInterval:7.5, 
                                  maxInterval:7.5});
      }).then(function(gatt) {
      console.log("Connected", gatt);
      show(0b100);
      return gatt.getPrimaryService(PRIMARY_SERVICE);
    }).then(function(service) {
      show(0b1000);
      console.log("Got Service");
      return service.getCharacteristic(READ_CHARACTERISTIC);
    }).then(function (characteristic) {
      show(0b100000);
      console.log("Got Characteristic");
      device = 'button1'; // how can we find this from gatt, a few promises ago?
      characteristic.on('characteristicvaluechanged', function(event) {
        parseMessage(device, event.target.value.buffer);
      });
      return characteristic.startNotifications();
    }).then(function() {
        nrfbusy = false;
    }).catch((error)=>{
        nrfbusy = false;
        console.log("Error: " + error);
        show("1   1\n"+
            " 1 1 \n"+
            "  1  \n"+
            " 1 1 \n"+
            "1   1\n");
    });
    }
    

    Additionally, when I try to connect to a second BLE device at the same time (e.g, run that connect() twice) I get ERR 0x12 (CONN_COUNT), so maybe the nrf52833 in the Microbit v2 can only connect to a single device at a time? Although https://devzone.nordicsemi.com/f/nordic-q-a/35158/how-many-connections-nrf52840-nrf52832-supports-as-peripheral suggests 20.

About

Avatar for user145265 @user145265 started