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,


    // When we click the connect button...
        function connectclick() {
          acceltimer = setInterval(accelcollect,650);
        function accelcollect() {
          Puck.eval("Puck.accel()",function(x) {
            //Get accel data
  • 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:­

  • 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.­367683/­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:­ools/blob/master/puck.js#L446

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

Detect when a puck.js disconnects from a browser form

Posted by Avatar for Jeff @Jeff