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

Identification of peripheral devices connected to central

Hello Everyone. 

                       I am a newbie in NRF52840 development. I have one NRF52840 device as a central device and many peripherals are getting connected to it (Max 6). Lets say 6 peripherals are connected to one central. I am not understanding how to identify the different peripheral devices on central side. There is p_gap_evt->conn_handle and &p_gap_evt->params.adv_report.peer_addr but I am not understanding how to use this to identify events from different peripherals??? Can anyone please help me on this? 

Parents
  • Hi,

    You are on to it. The key is the connection handle (conn_handle). Any event you get that relates to a specific connection, or any API function that operate on a specific connection, includes this handle. So if you are a central connected to 6 peripherals all will have unique connection handles.

    This starts when the connection is established and you get the BLE_GAP_EVT_CONNECTED. With that, you get a conn_handle which has a value. Say that value is 1. Then whenever you get another event where the conn_handle is 1, you know it is for the same peer device (peripheral in this case as you are making a central). If the conn_handle is 2 instead, you know it is for a different connection.

  • Thank you Einar. 

    so for 1st connection..conn_handle will be 1, 2nd connection conn_handle will be 2 and so on... lets say device 2 acquired conn_handle 2. what if conn_handle 2 disconnects in between?? when device 2 tries to reconnect again, it will acquire new conn_handle or it will connect with conn_handle 2? There is no need to check for peer address??

  • If it disconnects and connects again it will get a new connection handle. If you need to map it to a specific peer address you can do that by checking the address when it connects, and keep a map of conn_handle and address.

Reply Children
  • Thank you so much. Also what is device ID of a peripheral and how is it represented?

  • What do you mean by device ID? The only IDs you have is the connection handle, which is just a number without meaning, and with the BLE_GAP_EVT_CONNECTED event you get the peer's BLE address.

    (There is a concept of device ID in nRF devices, but that is not related to BLE, and is just a random number written into FICR during production in the two DEVICEID registers).

  • If i get a connection from BLE_GAP_EVT_CONNECTED and later I get data from a particular event in a different handle,then how to identify device. At that time i wont be having the same conn_handle I guess.?? My problem is I am getting event in a different handle and inside that I want to identify from which device i got the data.

  • sne_333 said:
    If i get a connection from BLE_GAP_EVT_CONNECTED and later I get data from a particular event in a different handle,then how to identify device

    You always get a connect handle with every event. You do not get all information with every event, though. So if you for instance need to map conn_handle to address later, you need to keep track of that.

    sne_333 said:
    At that time i wont be having the same conn_handle I guess.

    The conn handle is valid for the duration of the connection. And when the connection is established again, you know the address.

    Note that if the peer is a phone or something that use privacy, then you cannot map addresses over time, as it changes regularly. In that case, you will need to bond with the device in order to recognize it (using the IRK. This is handled for you by the peer manager library in that case, though.,

    sne_333 said:
    My problem is I am getting event in a different handle and inside that I want to identify from which device i got the data.

    That is exactly why all connection related events have the conn_handle. That is what you should use here.

  • Thank you so much...

    Ok so if I use "ble_evt_t const *p_ble_evt;" and "p_gap_evt->conn_handle" on other event handler, I will get the same handle number as well as the peer address right?

Related