Detect when a puck.js disconnects from a browser form

Posted on
  • Hello all,

    I am a newbie and appreciate any help. I have tried generating a solution but have lost much hair trying!

    I connect to the puck from a Windows browser using the following javascript function and query the accelerometer with a timer using the subsequent function. How can I detect if the puck.js disconnects?

    Thanks in advance,

    Jeff

    // When we click the connect button...
        function connectclick() {
          Puck.write('Puck.accelOn(1.6);\n');
          Puck.write('NRF.setTxPower(4);\n');
          acceltimer = setInterval(accelcollect,650);
        }
    
        function accelcollect() {
          Puck.eval("Puck.accel()",function(x) {
            //Get accel data
            accel.x=x.acc.x;
            accel.y=x.acc.y;
            accel.z=x.acc.z;       
          })
        }
    
  • Wed 2021.09.08

    Hi @Jeff   One idea might be to have the Puck regularly signal the web page, and have the web page continually look for that 'ping'. Within a try/catch block, when the ping doesn't arrive timely, throw and trap an exception within the web page itself.

    Example bi-directional comm examples here:

    https://www.espruino.com/Web%20Bluetooth­
    https://www.puck-js.com/go

  • Thanks for the information Robin. I will look into a try/catch block.

    Rather than looking for an error is could I query the BLE and make use of the following information the puck sends?

    Sorry if this is total newby question.

    puck.js:406 Connected
    puck.js:406 SEND ERROR: NotSupportedError: GATT operation failed for unknown reason.
    puck.js:406 Disconnected (gattserverdisconnected)

  • Sun 2021.09.12

    Hi @Jeff as I indicated 'One idea might be to have the Puck regularly signal' and you now have yet another.

    I steered away from that solution as I wasn't able to find a suitable example/tutorial and there had been a recent post on how to wire the rejected 'Promise' into existing code.

    http://forum.espruino.com/conversations/­367683/

    http://www.espruino.com/Reference#Blueto­othRemoteGATTService

    Anyone suggest/provide a simple example code block, other than the API reference?

  • Hi - those errors actually come from the puck.js library on the PC.

    I think the easiest way to check for connection is to use Puck.isConnected(). I believe Puck.getConnection().on('close', function() { console.log("connection closed!") }); will work too - but to need to do that after you connected the first time

    The Puck library isn't that well documented externally, but there are a bunch of comments inside it that might help: https://github.com/espruino/EspruinoWebT­ools/blob/master/puck.js#L446

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Detect when a puck.js disconnects from a browser form

Posted by Avatar for Jeff @Jeff

Actions