• I'm trying to prevent the puck from automatically reconnecting to a device that it has previously been connected to and paired with. So after the puck has been connected and paired with a device, I want to action a reset/power cycle of the puck and for it to not connect to the device again unless I go through the connection process again on the device.

    I know that I can just select "forget this device" from bluetooth settings on the device, but I want a way of implementing this from the puck itself (ie via a button press).

    My initial plan was to set it to create a device name containing a random string on init, and then calling a reset() on the button press. This plan failed as the phone/laptop device that the puck has been paired with ignores the fact that the bluetooth device name has changed and still reconnects. I assume it looks at the underlying MAC address instead.

    Does anybody know a way of implementing this?

    Perhaps there is a mode I can set which enables connection without pairing?

    Many thanks.

  • Just to make sure I understand - you're connecting to the Puck from a device like a PC or phone?

    You can write some code that runs on the connect event, looks at the address, and then calls NRF.disconnect()? http://www.espruino.com/BLE+Security#dis­connect-when-an-unknown-address-is-found­-whitelisting-

    However, it seems newer Android phones use a random MAC address for each connection so that may not work, and it's also possible they they will just repeatedly try to connect to the device.

    I don't know if this works for you, but you could actually change the MAC address of the Puck itself each time? That should stop the auto-reconnects: http://www.espruino.com/Reference#l_NRF_­setAddress

  • Thank you @Gordon.

    Just to make sure I understand - you're connecting to the Puck from a device like a PC or phone?

    That is correct I am using the Puck as a Ble HID Keyboard, I want to connect it to my phone, use it to pass a string of characters to the phone (based on an event), then (based on another event) I want to disconnect the Puck from the phone (without touching the phone) and have it not auto-reconnect to the phone.

    I had looked at changing the MAC address using each of the 3 variations as I wasn't sure from reading the documentation what each type of address was. It seemed a little confusing to me have a static address that is random?

    NRF.setAddress("xx:xx:xx:xx:xx:xx");
    NRF.setAddress("xx:xx:xx:xx:xx:xx public");
    NRF.setAddress("xx:xx:xx:xx:xx:xx random");
    
    

    But none of them seemed to have any effect on the device's mac address. I including the line in my onInit() function, I tried having it outside of the function, uploading my code and using the save() command. I have also tried entering it into the command line directly. When entering into the command line on the web IDE, it returns "undefined", then when I check for the change by entering NRF.getAddress(), it just returns the original default MAC address of the Puck. I have tried save(), on the device, then power cycling the puck, but the changes are still not working.

    If I can get the MAC address to successfully change with the NRF.setAddress, then this should work as a solution, because I can create a random string on init of the Puck, which will hopefully prevent reconnection to the phone as it will not appear to be a previously paired/bonded device.

    On another note, (although this wasn't a solution for me) I was able to change the advertised name with:

    NRF.setAdvertising({},{name:"Hello"});
    

    But I couldn't find a way of resetting this back to the default "puck.js xxxx" where I think the xxxx was the last few characters of the MAC address. Is there a command to reset this back to factory default?

  • Ahh, getAddress is always the hard-coded address, not the one you set with setAddress. Use something like NRF connect on a phone to scan for devices to see the real address.

    You want random addresses - there's a bit of info here: http://www.espruino.com/About+Bluetooth+­LE#connecting-puck-as-central-

    I just tried here and it works fine - but you do need to disconnect from BLE first with NRF.disconnect() before the changes take effect I imagine.

    Is there a command to reset this back to factory default?

    No - you can either reset() the device, or do something like :

    NRF.setAdvertising({},{name:"Puck.js "+NRF.getAddress().substr(-5).replace(":­","")});
    
  • So I'm still having no success with this:

    NRF.setAddress("yy:yy:xx:xx:xx:xx random");
    
    

    With yy:yy being the same as the original hardcoded MAC address to match the address type and avoid the INVALID_BLE_ADDR.

    The Puck still advertises and connects to my phone with the original hard-coded MAC address.

    The exact steps I have taken.
    I am using the web IDE in chrome. On the right hand side I have my BLE HID code (which is all functioning correctly). I connect the Puck to the web Ide. Upload my code from the RHS on to the puck. I then enter NRF.setAddress("C3:92:xx:xx:xx:xx random"); into the command on the left hand side. It returns undefined. I type save(). I type NRF.disconnect(). The Puck disconnects from the web ide. I restart the Puck, by lifting and releasing the battery. I then open the bluetooth settings on my android phone, scan for a new device, I find the Puck, connect and pair with it. I then click on the Puck in my Android bluetooth settings to check the MAC address and it displays the same hard-coded address that is returned when i type NRF.getAddress() into the ide. So as far as I can tell I am not successfully changing the MAC address.

    I just tried here and it works fine

    @Gordon, can you please tell me that exact steps you took to get the change to work

    I really appreciate your help on this Gordon.

  • can you please tell me that exact steps you took to get the change to work

    Sure. I connected with the Web IDE in Chrome, entered NRF.setAddress("f6:f6:f6:f6:f6:f6 random"); on the left-hand side of the IDE, disconnected, and then checked with NRF Connect on an Android phone and sure enough it had changed.

    NRF.setAddress will apply only until the device restarts (even after save) - so if you're resetting it by disconnecting the battery then yes, the old address will remain.

    If you add the NRF.setAddress command to the onInit function you should be fine though.

  • So, if I follow your steps above it works.
    But it doesn't work if I add it to the onInit.

    Here are the first few lines of my code:

    function onInit() {
    var kb = require("ble_hid_keyboard");
    NRF.setAddress("c3:92:e6:f6:f6:f6 random");
    NRF.setServices(undefined, { hid : kb.report });
    
    

    Also, when I add the setAddress to my onInit it seems to also prevent the HID service from starting, as when I connect to my android with the onInit as above it no longer connects as a BLE HID.

  • Ok, I just looked at this and it's to do with HID by the look of it.

    The setAddress call actually returns: INVALID_STATE which Nordic say is:

    The identity address cannot be changed while the roles are running.

    Which I believe means the address can't be changed while something like HID is set up.

    Just FYI, you can do:

    var err = "";
    function onInit() {
      LoopbackA.setConsole();
      LoopbackB.on('data',d=>err+=d);
    

    Then when you connect again you can see if there were any messages printed to the console.

    Anyway... if you try and set the address before (as you have done) then I believe adding HID requires a softdevice restart, which loses the new address :(

    So it may be possible for me to modify the firmware so it remembers that the address should be changed and then modifies it each time the softdevice restarts, but right now I think that setDevice on the current firmware won't work for you.

    However:

    I want to connect it to my phone, use it to pass a string of characters to the phone (based on an event), then (based on another event) I want to disconnect the Puck from the phone (without touching the phone) and have it not auto-reconnect to the phone.

    It seems from this that what you want is actually a lot simpler than that. I assumed you wanted to be able to connect with another device while stopping the phone connecting, but if you just want to stop any device connecting to Puck.js, simply use NRF.sleep().

    NRF.sleep() will disconnect Bluetooth and stop advertising (which makes it look like it's just disappeared/gone out of range). Just call NRF.wake() when you want to wake up and it'll start advertising and the phone will reconnect

  • OK, thanks @Gordon, so I think I understand the problem and why it won't work with the firmware as it is.

    It seems from this that what you want is actually a lot simpler than that.

    I'm afraid not.

    I assumed you wanted to be able to connect with another device while stopping the phone connecting

    This is correct. I want to connect the Puck to Phone A, send an HID string to it based on x-event, then, based on y-event I want the Puck to disconnect from Phone A or power-down and then be discoverable and able to connect to Phone B without Phone A ever auto-reconnecting.

    I assume that using an E.on function as well as an onInit won't solve my problem? as they are essentially the same thing?

    Is there a way to set the Puck to connect without bonding? Would that even work?

    Is there a way other than changing the MAC address of making the Puck appear to be a different/unknown device to a previously paired Phone?

    Can you think of any other ways around this other than a firmware update?

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

Preventing BLE reconnection after pairing. Single-use connection.

Posted by Avatar for tronic98776 @tronic98776

Actions