Testing for bluetooth connection

Posted on
  • Hello

    I'm using a puck as a ble HID keyboard with NRF.setServices(undefined, { hid : kb.report }); based on this Tutorial:

    What I'm looking for is a way to test if it is connected via bluetooth or not, for example to illuminate one of the LEDs when connected.

    I've looked through the documentation and searched the forum, but can't find anything.

    Help appreciated.


  • Wed 2019.08.21

    How about this link below heading 'advertising'


    which hyperlinks to


    Turning on LED beneath heading 'LEDs'


    Tutorials - See main page
    Menu >> Documentation >> Tutorial & Examples


  • Thank you for your reply Robin

    But I had looked previously at those sections, and I have looked at the tutorials and can't find a relevant one. I'm not sure what I'm missing in the documentation you linked to, could you please be more specific.

    Perhaps I didn't explain what I wanted to do, I know how to make the LEDs turn on and off, but I want to trigger an LED when a Central device connects to my Puck via BLE using the active Ble HID keyboard service. I am able to connect to the Puck and use it to send keyboard commands to the central device, but I want the connection to trigger a function (like illuminate an LED on the Puck).

    Thank you.

  • Could https://www.espruino.com/Reference#t_l_N­RF_HID be of help with this? I do not see any event that would report on-connected, but the HID / Puck could poll / solicit some response by sending something with no input effect or set and reset it right away that sends something and would then know that a connection exists.

    What I would expect and was looking for is some event on the set service... something like https://www.espruino.com/Reference#t_l_N­RF_connect, but on the particular service. It is worth to check what address this connected-callback receives: is it the computer's address?

  • Monitor devices that are within range, or have the specific device 'advertise'a content keyword and look out for that perhaps?

    It sounds like this is what you are after:


    Here is an example calling a function when an event occurs:


    Has Google with the 'site:' keyword specifier been attempted to narrow searches:

    hid ble puck site:espruino.com

    or forum specific

    hid ble puck site:forum.espruino.com

  • Thanks for your help.

    I've found a workaround, it functions, but its not ideal and I doubt it's very efficient in terms of battery power. I check the RSSI (Signal Strength), and illuminate the LED depending on the result:

    NRF.setRSSIHandler(function(rssi) {
      sigStrength = rssi;
    function checkRssi() {
    if (sigStrength > -51){
    else {digitalWrite(LED1,0);
    function myFunction() {
      myVar = setInterval(checkRssi, 3000);
  • ...nice, 'but' works for just any connection, does it? ...adding a check 'who' is on the other end of the connection can make sure no sniff phish has happened.

  • Edit: this (my suggestion) is not going to work for you. The callback fires immediately on upload, no connection required.

    Send some dummy data? This method offers a callback on send. But I don't know if this guarantees data is received, and by extension a connection. Suspect not.

    NRF.sendHIDReport(data, callback)


    The ble_hid_keyboard module uses it: https://www.espruino.com/modules/ble_hid­_keyboard.js

  • You can use NRF.connect/disconnect events to figure out when a central is connected to you or not: http://www.espruino.com/Reference#l_NRF_­connect

    I'd have thought that would do you, but you can also use the oddly named NRF.getSecurityStatus().connected to 'poll' as well: http://www.espruino.com/Reference#l_NRF_­getSecurityStatus

  • That almost does what I've been looking for. But is there a way to tell if the Espruino Web IDE is connected to a Bangle.js 2?

  • is there a way to tell if the Espruino Web IDE is connected to a Bangle.js 2?

    You mean instead of some other application? I'm afraid not - not unless you were to make your other application do something (send some data) to show that it isn't the IDE.

  • What I meant was, can a Bangle 2 app tell if the Web IDE is connected to it? print(...) sends stuff to the IDE's console, but it doesn't arrive if it's not connected. I was hoping there might be a way to detect if it had arrived or not, or at least if there was a connection.

  • I was hoping there might be a way to detect if it had arrived or not, or at least if there was a connection.

    You can't tell if it arrived, but as I'd mentioned in the post you replied to, you can just check NRF.getSecurityStatus().connected to see if you're connected?

  • I'm using that and hadn't known of it, so thanks for that.

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

Testing for bluetooth connection

Posted by Avatar for tronic98776 @tronic98776