This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Device manager: How to identify between already bonded phones with new phones?

Hi, I am developing a firmware using nrf51822xxaa s130 SDK 10.0. I am using device manager to exchange the security keys and storing the bond information. For my project, I need the firmware to connect to the phone that is already paired with the device (before power cycle of the device). In other words, I should be able to identify if the app has already bonded with me before or not. Also, I need to identify if this is the first time the app is bonding with me. [I remember I saw an event which would occur only the very first time when a device and app would bond and that event would not occur in the subsequent connects (because it is already bonded before). I cant remember which event is that.] The project is in its final stages and at this stage we cannot afford to make a transition to SDK 11.0 or later versions: please consider this. Please help. Thanks.

  • Hi Nordicdev,

    You can have a look at the device_manager.h and look for "Link Status Events", you can find the list of event regarding security process. You can also have a look here to see what happens under the hood between the softdevice and device manager.

    Basically you receive DM_EVT_LINK_SECURED after the link is encrypted (regardless it's a new central or not)

    You receive DM_EVT_SECURITY_SETUP if there is a bonding request

    You receive DM_EVT_SECURITY_SETUP_COMPLETE if it's a new bond or a refreshed bond (master require the new keys to be exchanged, rarely happens)

    If you want something more than that, you may need to look into the device manager code to extract information about if the central is bonded or not (check BLE_GAP_EVT_CONNECTED event in dm_ble_evt_handler() ).

  • Hi HungBui, Thanks for the reply. If the nrf51 device is previously bonded with the smart device, then, when the event BLE_GAP_EVT_CONNECTED happens in the dm_ble_evt_handler(), I do get device_instance_find returning an error code NRF_SUCCESS - IF I use Android, when I do the same experiment using iOS, device_instance_find returns error 5 which is NRF_ERROR_NOT_FOUND. The experiment I did was: erase the flash memory of nrf51. Forget the device in iOS. Connect iOS to nrf51 upon getting advertisement packets from nrf51. "Pair" is clicked once prompted. Disconnect the app. Reconnect the app again. This time, nRF51 does not recognise that iOS phone was previously bonded to it, but for Android, nrf51 returns error 0 when device_instance_find is called in on_connected event.

    Reason for the above is: in iOS, the peer addr in the connected event (under : device_instance_find(&p_ble_evt->evt.gap_evt.params.connected.peer_addr,) is not the same as the MAC addr of the iPad, but while storing the peer address after pairing is done (under: err_code = store_fn(&block_handle, (uint8_t *)&m_peer_table[p_handle->device_id], PEER_ID_SIZE, PEER_ID_STORAGE_OFFSET); inside device_context_store, the MAC addr is correct - meaning the MAC addr is the same as the iPAD's Bluetooth mac addr).

  • Also, for Android, when the nrf51 bonds with the phone for the first time, I receive DM_EVT_SECURITY_SETUP_COMPLETE, but I don't receive this when pairing with iOS? In other words, I am repeating the same original question (how to find if it is a new bond and how to find if the connected phone has previously been bonded with the nrf51 device) but for iOS smart devices. With Android, the answer that you have given works fine.

  • I have finally worked it out. For iOS, we cant rely on the "device_instance_find" which is used in "BLE_GAP_EVT_CONNECTED" under "dm_ble_evt_handler". This is because, in iOS, "The iPhone will change its public Bluetooth address every now and then, and only paired devices will have the correct key to resolve that public address and recognize the iPhone." reference: stackoverflow.com/.../ios-bluetooth-le-unable-to-connect-using-stored-pairing-data. So, the peer addr given by iOS in the on_connected event is actually fake. Used the following method that works for both iOS and Android: How to find if nRF and phone are bonding for the first time? As suggested by HungBai, if the events "DM_EVT_SECURITY_SETUP" and "DM_EVT_SECURITY_SETUP_COMPLETE" happen, that means, the app and nrf51 are bonding for the very first time. These events are not fired upon subsequent connects (as the security keys are already exchanged and both the nrf51 and the app remember the exchanged security keys). How to find if nRF51 and phone have already bonded before? If the "m_service_context_apply" returns success (under the event BLE_GAP_EVT_CONN_SEC_UPDATE), then, it means that the nRF51 and app have successfully bonded. Thanks, Hung Bai for shedding the light!

Related