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

How to know which peripheral sent data to central

We need to connect 2 peripherals with single central device.I have taken ble_app_multilink_central from SDK 15.3 and integrated the ble_app_uart_c in this. I am using the service ble_nus_c instead of ble_lbs_c.

My peripherals are sending periodic data at ~20 ms to the central. In the ble_nus_c_handler(), I want to know which peripheral has sent the data and take different action based on the source of the data. I read that this can be done using the p_ble_nus_evt->conn_handle. However, through the debugger, I found that when I get BLE_NUS_C_EVT_DISCOVERY_COMPLETE, the conn_handle is able to distinguish which peripheral caused this state, but when it gets to the periodic state BLE_NUS_C_EVT_NUS_TX_EVT on receiving packets from the peripheral, the conn_handle is always 0. So, for both devices, this same event is triggered and the conn_handle is of no use to distinguish the source of data.
What is the best way to know from exactly which peripheral the central has received the periodic data?

This is by init function:

BLE_NUS_C_ARRAY_DEF(m_ble_nus_c, NRF_SDH_BLE_CENTRAL_LINK_COUNT); //NRF_SDH_BLE_CENTRAL_LINK_COUNT set to 2
static void nus_c_init(void)
{
    ret_code_t       err_code;
    ble_nus_c_init_t init;

    init.evt_handler = ble_nus_c_evt_handler;

    err_code = ble_nus_c_init(&m_ble_nus_c[0], &init);
    APP_ERROR_CHECK(err_code);
	
    err_code = ble_nus_c_init(&m_ble_nus_c[1], &init);
    APP_ERROR_CHECK(err_code);	
}

  • The Connection Handle identifies which connection data came it on.

    So you would have to manage relating that to which particular peripheral in on which connection ...

  • Thanks for the reply. Can you please elaborate?

    As I mentioned in the question, in the periodic state BLE_NUS_C_EVT_NUS_TX_EVT, on receiving packets from the peripheral, the conn_handle is always 0, even if the event is triggered by different peripherals. So, every instance we get this periodic event, how do you know which peripheral is the source of this data?

    Do I need to have separate event handlers for the 2 peripherals? In that case how to initialize and how to handle it in the db_disc_handler()?

  • Have you looked at the Mulit Link example?

    This worked for me:

    /**@brief Function for handling data received by the Nordic UART Service.
     *
     */
    static void ble_nus_on_rx_data(uint16_t conn_handle, uint8_t * p_data, uint16_t data_len)
    {
        uint8_t slot = find_slot_from_conn_handle( conn_handle );
    
        NRF_LOG_DEBUG("Rx Handle %d; slot %d:", conn_handle, slot );
        NRF_LOG_HEXDUMP_DEBUG(p_data, data_len);
    
        // m_ble_nus_c[conn_handle] identifies the connection on which the data arrived

    the "slot" is how I identify particular peripherals.

Related