• I am trying to implement the code in the example in this link: https://www.espruino.com/Puck.js+Controlling+Other+Pucks

    I have two Puck.js v1.0 running firmware 2v09. I flashed one with the "peripheral" code and saved it to flash before disconnecting from the Web IDE and power-cycling the Puck. I flashed the other with the "central" code with a few added debug statements in the catch blocks to print out the error.

    When I press the button on the central puck, my the LED flashes red to indicate error and the error is printed to the console: BLE error 0x11 (BUSY). After that, any button presses just report the error "No device found matching filters" until I power-cycle the peripheral Puck. I assume my central puck is successfully connecting but not disconnecting when the error is caught (even though I call the disconnect()) function. This leaves the peripheral Puck in an odd state that can only be fixed with a power-cycle.

    My question is twofold:
    1) Where am I going wrong with the sample code (in the link above) because my peripheral Puck is always busy?
    2) How can I disconnect cleanly on error so I don't have to power-cycle my peripheral Puck every time?

    Since I modified the example code (to add debug statements in the catch() blocks), here is my code:

    // Are we busy?
    var busy = false;
    
    // The device, if we're connected
    var connected = false;
    
    // The 'tx' characteristic, if connected
    var txCharacteristic = false;
    
    // Function to call 'toggle' on the other Puck
    function sendToggle() {
      if (!busy) {
        busy = true;
        if (!connected) {
          NRF.requestDevice({ filters: [{ name: 'Puck.js ....' }] }).then(function(device) {
            return device.gatt.connect();
          }).then(function(d) {
            connected = d;
            return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
          }).then(function(s) {
            return s.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
          }).then(function(c) {
            txCharacteristic = c;
            busy = false;
            // Now actually send the toggle command
            sendToggle();
          }).catch(function(e) {  
            console.log("error: ", e);
            connected=false;
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
            if (connected) connected.disconnect();
          });
        } else {
          txCharacteristic.writeValue("toggle()\n").then(function() {
            digitalPulse(LED2, 1, 500); // light green to show it worked
            busy = false;
          }).catch(function(e) {
            console.log("error: ", e);
            digitalPulse(LED1, 1, 500); // light red if we had a problem
            busy = false;
            if (connected) connected.disconnect();
          });
        }
      }
    }
    
    // Call sendToggle when the button is pressed
    setWatch(sendToggle, BTN, { edge:"rising", debounce:50, repeat: true });
    

    As additional information, the example code in the following documentation articles also fails with a BLE error 0x11 (BUSY) message.

    1. https://www.espruino.com/Reference#l_NRF_requestDevice
    2. https://www.espruino.com/Reference#t_l_NRF_connect

    The sample code in the documentation here: https://www.espruino.com/Reference#t_BluetoothDevice does execute without error. This is leading me to believe that my two Pucks can connect to each other but when I try to use the UART service to send commands, the BLE error 0x11 (BUSY) error is thrown.

About

Avatar for Kymvaris @Kymvaris started