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 Reply Children
  • 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!

Related