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

Is it safe to use conn_handle as an index to an array?

In other words, is it safe to assume that in various BLE events, conn_handle is either invalid (0xFFFF) or between 0 and (CENTRAL_LINK_COUNT + PERIPHERAL_LINK_COUNT - 1), as used in softdevice_enable_get_default_config()?

I've seen this used in nRF5_SDK_12.2.0_f012efa\examples\ble_central\ble_app_multilink_central\main.c, but not in the documents.

  • Hi,

    What is your purpose of doing that? I have seen a similar question where the purpose was to store information about connected devices in an array and later use the connection handle as an index to access the information in the array. The problem with this though, is that sometimes it takes time for the SoftDevice to detect a disconnection (supervision timeout) and until the supervision timeout kicks in, the connection handle is not reused. So consider this:

    1. A device connects to our SoftDevice and gets assigned a connection handle.

    2. The device moves away or there is some kind of link loss.

    3. If the same device immediately reconnects before supervision timeout then the same device will get a new connection handle. That's how you will end up with the same device having 2 different connection handles.

    Hence, you have to be very careful if you want to do this.

  • Hi Martin,

    I aim to support multiple, simultaneous connections as done in the example I mentioned. In my case, I need to keep an array of NUS service contexts (m_nus_cs).

    Regarding the scenario you mentioned, won't I get an event if the link is lost? If so - I will use it to clear the corresponding context.

    Just to make sure - can conn_handle for a given connection change without the application being notified? If so this poses a real problem to any client relying on the softdevice.

  • Hi, see my answer here on how you should handle multiple connections with the nus_c service.

Related