• Gordon - thanks. Glad to confirm I wasn't missing the obvious. It's good to learn how to do these things.

    This relates to the thread above, so I hope I can continue it here rather than start a new thread. I can't find the answer in the forum.

    Getting an experiment using GATT between Puck and Pixl has been quite nice and easy - Got a Puck sending notifications to the Pixl every few minutes - managed to graph the temperature last night - until it stopped working. It looks as though the devices were a little bit too far apart so interference or whatever caused them to disconnect, as it's OK when they are closer. When too far apart the Pixl stops working with an error:

    Uncaught InternalError: BLE task completed that wasn't scheduled (3/0)

    I figured that I wasn't handling the disconnection that occurred, so I handled the 'gattserverdisconnected' event. This handler is doing something: my log function is called and prints reason '8', so is doing something.

    However I still get the error "Uncaught InternalError: BLE task completed that wasn't scheduled (3/0)"

    Is there something I should do to handle this and stop the Uncaught error? I tried adding a handler in NRF.on('disconnect')... but that doesn't seem to be called - I'm not sure why.

    I'm also unsure of how best to handle this case. I basically always want the two devices to connect when they can, so am making the Pixl periodically try to reconnect.

    For now, I've done this as follows:

    • Added a 'connected' flag which is set false initially.
    • Added a setInterval call to call the function that does the connection - to keep trying to connect until a successful connection is made.
    • When a device is connected the flag is set to True.
    • The gattserverdisconnected event sets 'connected' to false, so the timer function tries to periodically reconnect again.
    • I guess I should set and clear the interval rather than using a flag, but - ease of experiment.

    Is this the neatest way of handling this scenario? It looks as though the Uncaught Internal error is harmless, but it would be cleaner for me to do is correctly and remove the error.

    Code below shown - this connects to the puck running the service above.

    Thanks!

      NRF.on('disconnect', function () {
        console.log("NRF disconnected");
      });
    
      setInterval(function() {
        if (!connected) {
          console.log("Trying to connect...");
          connectRemoteTemperature(onTemperatureRe­ceived);
        }
      }, 3000);
      
      // Do Bluetooth stuff to connect to the first device found with name beginning
      // 'Puck' and with our service characteristics
      function connectRemoteTemperature(newTextRxedFunc­tion) {
        NRF.requestDevice({ filters: [{ namePrefix: 'Puck' }]
                      }).then(function(device) {
          console.log("Found Puck",device);
    
          return device.gatt.connect();
        }).then(function(g) {
          console.log("Connected");
          connected = true;
          g.device.on('gattserverdisconnected', function(reason) {
             console.log("GATT server disconnected ",reason);
             connected = false;
          });
    
          return g.getPrimaryService("047b0001-37e3-457d-­3763-2a9b2d24cbb2");
        }).then(function(service) {
          console.log("Got service");
          return service.getCharacteristic("047b0002-37e3­-457d-3763-2a9b2d24cbb2");
        }).then(function(characteristic) {
          console.log("Got characteristic");
          // Pulse the light to show we're connected
          digitalPulse(LED,1,[100,0,100,0,100]);
          
          // Called when the value updates. Decode the data from the event
          characteristic.on('characteristicvaluech­anged', function(event) {
              var dataview = event.target.value; // Gives us a JS dataview
              var resultArray = dataview.buffer; // Gives us an array of chars
              var text = String.fromCharCode.apply(String, resultArray); // Gives us a string
              newTextRxedFunction(text); // Call the handler function to update the graph/whatever
        });
    
        return characteristic.startNotifications();
        }).catch(function(e) {
          console.log("ERROR",e);
        });
      }
    
    
About

Avatar for ColinP @ColinP started