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

What is the point of connection handles?

I notice that for everything related to a BLE connection in the nRF code, there is a "connection handle". Usually, the handle is zero. When is the connection handle NOT zero? I ask because my code looks messier if I have to keep an array of connection handles. If my nRF is just a peripheral, then it can only have one connection at once, right? A peripheral stops advertising upon connection, so it can never have more than one connection, right? 

I am trying to look up the data length for my current connection too. Where do I do that? Do I make an event handler like this and store the data length separately, or can I look it up on-demand with my connection handle? 

/**@brief Function for handling events from the GATT library. */
static void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
{
    (void) p_gatt; //UNUSED 
    uint16_t              conn_handle = p_evt->conn_handle;

    switch (p_evt->evt_id)
    {
        case NRF_BLE_GATT_EVT_ATT_MTU_UPDATED:
        {

            NRF_LOG_INFO("ATT MTU exchange completed. MTU set to %u bytes.",
                         p_evt->params.att_mtu_effective);

            NRF_LOG_INFO("Data length is %u bytes.", p_evt->params.data_length);
        } break;

        case NRF_BLE_GATT_EVT_DATA_LENGTH_UPDATED:
        {
            NRF_LOG_INFO("Data length updated to %u bytes.", p_evt->params.data_length);
        } break;
    }
}

Parents
  • A peripheral stops advertising upon connection, so it can never have more than one connection, right? 

    This is not correct, a peripheral can maintain up to 20 link simultaneously. Take a look at the BLE Multiperipheral Application example, which shows how a peripheral can connect to 4 centrals. This application uses the Connection state Library to keep track of the states for the differenc connections. It is also possible to use the BLE Link Context Manager on top of that library, and enables you to associate link-related data to a given connection handle. All in all, connection handles are quite useful, since you can keep control and differentiate between the connections. 

    When is the connection handle NOT zero?

    The connection handle is not zero if it is initalized but not associated with a connected device, then it is assigned the value 0xFFFF. Otherwise, if you are working with a multilink application, for example the BLE Multiperipheral Application, all the connection handles except for the first will have a non-zero value. E.g. if you are connected to four different central devices, their connection handles will be assigned the values 0x0, 0x1, 0x2 and 0x3 respectively.

    Best regards,

    Simon

Reply
  • A peripheral stops advertising upon connection, so it can never have more than one connection, right? 

    This is not correct, a peripheral can maintain up to 20 link simultaneously. Take a look at the BLE Multiperipheral Application example, which shows how a peripheral can connect to 4 centrals. This application uses the Connection state Library to keep track of the states for the differenc connections. It is also possible to use the BLE Link Context Manager on top of that library, and enables you to associate link-related data to a given connection handle. All in all, connection handles are quite useful, since you can keep control and differentiate between the connections. 

    When is the connection handle NOT zero?

    The connection handle is not zero if it is initalized but not associated with a connected device, then it is assigned the value 0xFFFF. Otherwise, if you are working with a multilink application, for example the BLE Multiperipheral Application, all the connection handles except for the first will have a non-zero value. E.g. if you are connected to four different central devices, their connection handles will be assigned the values 0x0, 0x1, 0x2 and 0x3 respectively.

    Best regards,

    Simon

Children
No Data
Related