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

How to switch between connection handles when nrf52 is connected to multiple centrals?

Hi,

I am using nRF52 and SDK13 with SoftDevice 132 v 4.0.2 which is now supporting multi-peripheral connections. I am using multi-peripheral example for reference. In that i have connected 4 android devices to nRF52. I want to send data from nRF52 to each of connected device. So i think I need to switch between connection handles to send data..How to achieve this?

  • Well if the data is just a readable characteristic then there's nothing to do, you set the value, any of the connected centrals can read. if you're talking about something like notifications where you send data out proactively, those calls all take a connection handle, so just put in the connection handle of the device you want to send to.

  • Hi,Thanks RK when I am simply sending the data it is being received by lastly connected device,not by others...as you have mentioned in second case from where I should extract connection handle of each connected device?

  • The connection handle is given to you when the device connects. You need to save it and use it later.

  • I also have some question about this. Assume my application can handle 4 concurrent connections and i configure the softdevice for this. Will the connection handles always be in range [0:3]?

    If i have a structure (this is just an example) that holds the state of a client, and have an array of these structures, is it safe to use the connection handle as index in the array?

    typedef struct ble_client_s {
        volatile uint8_t     is_connected;
        volatile uint8_t     notification_enabled;
        app_fifo_t  rx_queue;
        app_fifo_t  tx_queue;
        void        *p_context;
    } ble_client_t;
    
    static ble_client_t m_ble_clients[4];
    
    static void isr_ble_evt_handler(ble_evt_t *p_ble_evt)
    {
        switch (p_ble_evt->header.evt_id)
        {
            case BLE_GAP_EVT_CONNECTED:
            {
                m_ble_clients[p_ble_evt->evt.gatts_evt.conn_handle].is_connected = 1;
            } break; /* BLE_GAP_EVT_CONNECTED */
            case BLE_GAP_EVT_DISCONNECTED:
            {
                m_ble_clients[p_ble_evt->evt.gatts_evt.conn_handle].is_connected = 0;
            } break; /* BLE_GAP_EVT_DISCONNECTED */
    
            /* ... */
    
            default:
                break;
    
        }
    }
    

    If this a suitable solution to hold states of the different connected clients? Or can the connection handles have other values?

  • no it's not - there's no guarantees abut the connection handle value. It might work today, it might fail dismally tomorrow with a new release. For full code compatibility you need to map them.

Related