Check if Puck is connected with MDBT (Bluetooth)

Posted on
  • Hi Gordon,

    how I can implement the NRF.on("connect") service to check, if there is a bluetooth device connected or not.

    I just want to send signals from Puck to MDBT when they are connected.
    I worked with a variable "connected" and set it to true or false.

    I have to setWatch functions with the Puck button for rising and falling. When I connect and disconnect it via the Puck button all is working.

    But now I removed the battery from the MDBT and the Puck haven´t detected it as disconnected.

    var log = print;
    var uart;
    var status = 0;
    var l;
    var t;
    var counter = 1;
    var connected = false;
    var kb = require("ble_hid_keyboard");
    NRF.setServices(undefined, { hid : kb.report }); 
    
    
    
    
    ///Mode Flashing
    
    
         setWatch(function(c) {
    
           
           
         if (connected == true) {
    
           
         setTimeout(function () {
    
         if (counter == 1){clearInterval(); digitalWrite (LED2, 1);
         uart.write("digitalWrite(LED2,1), digitalWrite(D19, 1);\n");
         }
    
         if (counter == 2){clearInterval(); digitalWrite (LED2, 1);
         uart.write("digitalWrite(LED2,1), digitalWrite(D17, 1);\n");
            }
    
         if (counter == 3){clearInterval(); digitalWrite (LED2, 1);
         uart.write("digitalWrite(LED2,1), digitalWrite(D15, 1);\n");
            }
    
         if (counter == 4){clearInterval(); digitalWrite (LED2, 1);
         uart.write("digitalWrite(LED2,1), digitalWrite(D18, 1);\n");
            }
    
         if (counter == 5){clearInterval(); digitalWrite (LED2, 1);
         uart.write("digitalWrite(LED2,1), digitalWrite(D16, 1);\n");
            }
    
         if (counter == 6){clearInterval(); digitalWrite (LED2, 1);
         uart.write("digitalWrite(LED2,1), digitalWrite(D14, 1);\n");
            }
         }, 1000);
         }
         }, BTN, { repeat: true, edge: 'rising' });
    
    
         setWatch(function(c) {
     
           
           if (connected == false) {
        kb.tap(kb.KEY.ENTER, 0);
       }   
           
           
         if (connected == true) {
    
         var l = c.time-c.lastTime;  
    
         if ((c.time-c.lastTime > 0.3) && (c.time-c.lastTime < 1.0)) 
         {counter = counter + 1;}
    
         if (counter > 6)
         {counter = 1;}
    
    
          log("Counter:");
          log(counter); 
    
          if (counter == 1)
          {clearInterval();
          setInterval("{digitalPulse (LED2, 1, 50)}", 5000);}
    
          if (counter == 2)
          {clearInterval();
          setInterval("{digitalPulse (LED2, 1, [50, 300, 50])}", 5000);}
    
          if (counter == 3)
          {clearInterval();
          setInterval("{digitalPulse (LED2, 1, [50, 300, 50,  300, 50])}", 5000);}
    
          if (counter == 4)
          {clearInterval();
          setInterval("{digitalPulse (LED2, 1, [50, 300, 50,  300, 50,  300, 50])}", 5000);}
    
          if (counter == 5)
          {clearInterval();
          setInterval("{digitalPulse (LED2, 1, [50, 300, 50,  300, 50,  300, 50,  300, 50])}",         5000);}
    
          if (counter == 6)
          {clearInterval();
          setInterval("{digitalPulse (LED2, 1, [50, 300, 50,  300, 50,  300, 50,  300, 50,           300, 50])}", 5000);}
    
         if (counter == 1){digitalWrite (LED2, 0);
         uart.write("digitalWrite(LED2,0), digitalWrite(D19, 0);\n");
         }
    
         if (counter == 2){digitalWrite (LED2, 0);
         uart.write("digitalWrite(LED2,0), digitalWrite(D17,0);\n");
            }
    
         if (counter == 3){digitalWrite (LED2, 0);
         uart.write("digitalWrite(LED2,0), digitalWrite(D15, 0);\n");
            }
    
         if (counter == 4){digitalWrite (LED2, 0);
         uart.write("digitalWrite(LED2,0), digitalWrite(D18, 0);\n");
            }
    
         if (counter == 5){digitalWrite (LED2, 0);
         uart.write("digitalWrite(LED2,0), digitalWrite(D16, 0);\n");
            }
    
         if (counter == 6){digitalWrite (LED2, 0);
         uart.write("digitalWrite(LED2,0), digitalWrite(D14, 0);\n");
            } 
         }
              }, BTN, { repeat: true, edge: 'falling' });
    
    //BLE Connect
    
        function flag(e) {
      
        var l = e.time-e.lastTime;  
        if ((e.time-e.lastTime < 0.3) && (connected == false)){
        log("Sending command");
        log("Searching for device...");
        NRF.requestDevice({ filters: [{ namePrefix: 'MDBT42Q' }] }).then(function(device) {
        log("Connecting...");
        device.on('gattserverdisconnected', function() {
        });
        return require("ble_uart").connect(device);
        }).then(function(u) {
        log("Connected");
        digitalPulse(LED3, 1, 300);
        uart = u;
        connected = true;
          counter = 1;
        });}
          
    ///BLE Disconnect
      
        
         setTimeout(function(device) {
         
         if (connected == true){ 
         uart.disconnect();
         log("Disconnected");
         connected = false;
         counter = 1;
         digitalPulse (LED3, 1, [50, 300, 50]);
         clearInterval();
         }
         }, 60000);
         
        if ((e.time - e.lastTime < 0.3) && (connected == true) ){
        setTimeout(function(device) {
        uart.disconnect();
        log("Disconnected");
        connected = false;
                      
        if (connected == false){
        clearInterval();
        digitalPulse (LED3, 1, [50, 300, 50]);
        digitalWrite(D14, 0);
       ...
    
    

    Best regards

  • But now I removed the battery from the MDBT and the Puck haven´t detected it as disconnected.

    How long did you leave it? There's a bit of a timeout in the connection to allow for radio interference, so it won't log it as soon as the connection is gone - but it should after maybe 10 seconds.

  • Let me check. Thanks for the info.

  • Cool, that works. Around 30 Seconds but thats ok for me!

    Thanks Gordon!

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

Check if Puck is connected with MDBT (Bluetooth)

Posted by Avatar for Blackhorse @Blackhorse

Actions