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:
    https://www.espruino.com/BLE+Keyboard

    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.

    Thanks

  • Wed 2019.08.21

    How about this link below heading 'advertising'

    https://www.espruino.com/About+Bluetooth­+LE

    which hyperlinks to

    https://www.espruino.com/BLE%20Advertisi­ng

    Turning on LED beneath heading 'LEDs'

    https://www.espruino.com/Puck.js#leds



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

    https://www.espruino.com/Tutorials

  • 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:

    http://www.espruino.com/Reference#l_NRF_­findDevices

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

    http://forum.espruino.com/comments/13694­597/



    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){
      digitalWrite(LED1,1);
    }
    else {digitalWrite(LED1,0);
    }
    }
      
    function myFunction() {
      myVar = setInterval(checkRssi, 3000);
    }
      
    myFunction();
    
    
  • ...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)
    

    https://www.espruino.com/Reference#l_NRF­_sendHIDReport

    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

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

Testing for bluetooth connection

Posted by Avatar for tronic98776 @tronic98776

Actions