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

How to map a gap event to a physical device?

What unique information can I use to know which physical device disconnected for example? Say I have a MAC address or a name encoded into the advertising packet. Correct me if I'm wrong, but connection handles are created after a connection is established. So how can I map a mac address or name to a connection handle?

  • Yes, in theory information like the peer address will be returned along with the connection handle in the connection event. Therefore, in theory I should know the peer address the connection handle is associated with. However, I have looked at the peer address, and the bytes of the address array are all zero which are non-random numbers.... The caveat is that I have only looked at the peer address in the disconnection event. It's possible that the peer address is non-zero in the connection event, but I can't confirm this because I am not in the office. I still find it odd that the peer address is all zero bytes in the disconnection event. I use the same function to print the peer address in the advertisement packet that I use to print the peer address in the disconnect event, so I am confident that my code for printing the peer address is correct.

  • There is no peer address in the disconnect event. All the structure for that event (BLE_GAP_EVT_DISCONNECTED -> ble_gap_evt_disconnected_t) has is a reason code.

    There is however a peer address in the connection event (BLD_GAP_EVT_CONNECTED -> ble_gap_evt_connected_t).

    And all of this doesn't matter anyway since you can only as central get a connection event for the thing you asked the softdevice to connect to so if you knew what it was before you asked to connect, you know what is it when you get the connection callback and you can associate that handle with it.

  • This is bad news. Are you saying that the disconnect event doesn't event contain the handle of the terminated connection? So there is no way to know which device is disconnected if I have multiple devices which can disconnect randomly?

  • You can get which connection handle generated BLE_GAP_EVT_DISCONNECTED, see how it is treated in examples\ble_central\ble_app_multilink_central\main.c SDK example. Actually search always for examples (by full-text search in *.c and/or *.h file) in SDK prior to asking;)

  • No I said nothing of the sort. Every BLE event has a connection handle as you sboulder know from the code you’ve been writing. I said the event structure for the disconnect event only has a reason field.

    At this point you need to sit down and read the documentation properly because you’re just guessing your way through. Read the message sequence charts and look at the event types and corresponding structures until you understand the flow.

Related