Detecting web bluetooth disconnection

Posted on
  • If a puck disconnects from my browser due to the connection dropping out (e.g. it moves out of range) is there a way to detect this on the web front end? I can see that puck.js creates the following console log:
    Disconnected (gattserverdisconnected)

    I’m using the following code snippet to create a 2-way connection as per one of your site tutorials. This includes the part about resetting the puck on disconnection, which seems to work fine on the puck - I just want to be able to reset the browser too.

    Puck.connect(function(connection) { 
        connection.on("data", function(d) { 
            // handle receiving data 
        connection.write("reset();\n", function() { 
                // do various stuff here 
                // reset on disconnect 
                connection.write("NRF.on('disconnect',fu­nction(){ reset(); });\n",function(){}); 
        // I'm hoping there's a way to detect disconnection here 
        // e.g. connection.on("disconnect", function(){}); 


  • Looking at the libray I think connection.on('close', function() { ... }); would do it...

  • Thanks, that seems to do the job!
    Might be worth noting, though, that it doesn't appear to work every time - sometimes the connection can drop out, and no close event is fired. When this happens, the "Disconnected (gattserverdisconnected)" log does not appear in the console from puck.js either.

  • Ok, thanks - does any other message about disconnection appear that might mean the library is at all aware the connection has dropped out? You can set Puck.debug=3; to help with extra logging info.

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

Detecting web bluetooth disconnection

Posted by Avatar for primalanomaly @primalanomaly