Multilink Central/Peripherals ID

Hello everyone,

I am working with the SDK v17.1.0 with the DK nRF52832 with the examples of ble_app_uart and ble_app_uart_c.

I want to have several peripheral boards that send data to a central board.

But I also want to have control over the slave I am questionning, so I want each slave to have its own ID. 

First I looked on 

Thank you everyone!

Parents
  • Hello,

    Each peripheral gets assigned a connection handle on connection, and it's then up to the application to keep track of which peripheral each handle belongs to. One way to do this is to map the connection handle to the peripheral's BLE address in the NRF_BLE_SCAN_EVT_CONNECTED event.

    Best regards,

    Vidar

  • Hello, thank you for your answer. 

    Yes, I could put an ID for each handle, but these ID will not represent the slave physically. 

    What I mean is that if Slave 1 is connected, I will attribute the ID 0 and then, Slave 2 will have the ID 1. If I restart the experiment, if Slave 2 is connected first, it wil have the ID 0 and not 1. Same goes with Slave 1. 

    Do you have an idea to identify every slave? 

    Thank you

  • Hello, 

    Indeed, I saw that I can retrieve the MAC address of each peripheral (Which should be unique for each peripherals) as printed in the NRF_BLE_SCAN_EVT_CONNECTED in the ble_app_uart_c.

    For the mapping, I was thinking about creating an 2D array and correspond each column to one handle and a MAC address. 

    However, I am not sure how to retrieve the handler ID as handle ID seems to be defined during a gap event and I am in a scan event. I don't quite understand how the of the handles are managed. 

    Thank you very much! 

  • Hello,

    Yes, an array should be fine. Here is how you can retrieve the connection handle from the NRF_BLE_SCAN_EVT_CONNECTED event:

             case NRF_BLE_SCAN_EVT_CONNECTED:
             {
                  ble_gap_evt_connected_t const * p_connected =
                                   p_scan_evt->params.connected.p_connected;
                  
                 NRF_LOG_INFO("Connection handle 0x%x",
                              p_scan_evt->params.connected.conn_handle);
    
                 // Scan is automatically stopped by the connection.
                 NRF_LOG_INFO("Connecting to target %02x%02x%02x%02x%02x%02x",
                          p_connected->peer_addr.addr[0],
                          p_connected->peer_addr.addr[1],
                          p_connected->peer_addr.addr[2],
                          p_connected->peer_addr.addr[3],
                          p_connected->peer_addr.addr[4],
                          p_connected->peer_addr.addr[5]
                          );
             } break;

    Also, remember to update the array when you get disconnected (BLE_GAP_EVT_DISCONNECTED) from a peripheral.

    Best regards,

    Vidar

  • Hello, thank you for the answer. 

    I am having trouble to built the array. In one hand, I have the conn_handle which is a hexadecimal value  that I need to put on the first line of the array and on the other hand, I have the MAC address of 6 bytes that I need to put on the second line of the array. 

    I tried to create my own type based on a char but I only print nonsense

    Hee is the code I tried:

    #define MAX_MAC_HANDLE_MAP_LENGTH 100
    typedef char addr;
    static addr mac_handle_map[2][MAX_MAC_HANDLE_MAP_LENGTH] = {"0"}; /**< Array of MAC address corresponding with connection handle
    with 20 devices max */

    static addr mac_addr_str[6], conn_handle_str[6];
    sprintf(mac_addr_str, "%02x%02x%02x%02x%02x%02x",
    p_connected->peer_addr.addr[0],
    p_connected->peer_addr.addr[1],
    p_connected->peer_addr.addr[2],
    p_connected->peer_addr.addr[3],
    p_connected->peer_addr.addr[4],
    p_connected->peer_addr.addr[5]
    );
    sprintf(conn_handle_str, "%x", p_scan_evt->params.connected.conn_handle);
    //NRF_LOG_INFO("%s\n\n\n", conn_handle_str);
    //NRF_LOG_INFO("%s\n\n\n", mac_addr_str);

    mac_handle_map[0][p_scan_evt->params.connected.conn_handle] = conn_handle_str;
    mac_handle_map[1][p_scan_evt->params.connected.conn_handle] = mac_addr_str;

    NRF_LOG_INFO("Handler %s with MAC address %s\n\n\n",
    mac_handle_map[0][p_scan_evt->params.connected.conn_handle],
    mac_handle_map[1][p_scan_evt->params.connected.conn_handle]);

  • Hello,

    Would something like this work:

    static ble_gap_addr_t m_mac_handle_map[NRF_SDH_BLE_TOTAL_LINK_COUNT];
    
    /**@brief Function for handling Scanning Module events.
     */
    static void scan_evt_handler(scan_evt_t const * p_scan_evt)
    {
        ret_code_t err_code;
    
        switch(p_scan_evt->scan_evt_id)
        {
             case NRF_BLE_SCAN_EVT_CONNECTING_ERROR:
             {
                  err_code = p_scan_evt->params.connecting_err.err_code;
                  APP_ERROR_CHECK(err_code);
             } break;
    
             case NRF_BLE_SCAN_EVT_CONNECTED:
             {
                 
                 uint16_t conn_handle = p_scan_evt->params.connected.conn_handle;
                 ble_gap_evt_connected_t const * p_connected =
                                   p_scan_evt->params.connected.p_connected;
                  
                 NRF_LOG_INFO("Connection handle 0x%x",
                              p_scan_evt->params.connected.conn_handle);
                
                
                 memcpy(&m_mac_handle_map[conn_handle], &p_connected->peer_addr,
                    sizeof(m_mac_handle_map[0]));
    
    
                 // Scan is automatically stopped by the connection.
                 NRF_LOG_INFO("Connecting to target %02x%02x%02x%02x%02x%02x",
                          p_connected->peer_addr.addr[0],
                          p_connected->peer_addr.addr[1],
                          p_connected->peer_addr.addr[2],
                          p_connected->peer_addr.addr[3],
                          p_connected->peer_addr.addr[4],
                          p_connected->peer_addr.addr[5]
                          );
             } break;
             ...

    ? I'm simply using the connection handle as the array index. I'm also not converting the address to a hex string.

  • Hello,

    Yes it works fine, thank you very much!

Reply Children
No Data
Related