Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

UART Bridge between Central and Peirpheral

Hello,

        I want to make a simple UART Bridge between Central and Peirpheral. I'm usign nRF52832 with nRF5_SDK v15. There are central and peripheral uart examples in the SDK. 

        I can connect to any mobile phone by peripheral example. However I can not work with the central example.

        How can I connect to a peripheral device by using central example ? How will I select the device to connect ? do I need to write a static MAC address or UUID ?

        When I run the central example, I just see the string on the uart line. "BLE UART central example started."

  

Best Regards  

  • Hello,

    The NUS service UUID is defined in ble_nus_c.h (NUS_BASE_UUID + BLE_UUID_NUS_SERVICE)

    "In case there are two BLE devices nearby, Which one is selected by the central to connect ?How can I connect the one that I want to. In short, I just want to seach for the specific UUID devices nearby, and then I will select one of them to connect."

    The central example will connect to the first device it sees, and the decision to connect is made in main.c::on_adv_report(). So the on_adv_report() needs to be modified if you want to connect to a device based on user input. You could use the UART interface to display discovered devices and receive user input. 

  • Hello

          As I see from the code in the function on_adv_report() it connect to peripheral. However I can not understand How should be the code structure in order to establish a specifically selected peripheral BLE device. 

    p_adv_report->peer_addr.addr keeps the address of the peripheral device. 

    ble_advdata_uuid_find() this function is called one time. if it finds a connectible device, it connect. never called again.

    How can I list the nearby devices? I do not want to connect at first found device. 

  • I have modified the on_adv_report() function. Now it prints all BLE devices nearby.

    When I feed the selected BLE device addres to array p_adv_report->peer_addr, will it connect to that device ?

    static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
    {
        ret_code_t err_code;
    
        if (ble_advdata_uuid_find(p_adv_report->data.p_data, p_adv_report->data.len, &m_nus_uuid))
        {
            //err_code = sd_ble_gap_connect(&p_adv_report->peer_addr,
            //                              &m_scan_params,
            //                              &m_connection_param,
            //                              APP_BLE_CONN_CFG_TAG);
    
            //if (err_code == NRF_SUCCESS)
            {
                // scan is automatically stopped by the connect
                err_code = bsp_indication_set(BSP_INDICATE_IDLE);
                APP_ERROR_CHECK(err_code);
                //NRF_LOG_INFO("Connecting to target %02x%02x%02x%02x%02x%02x",
                NRF_LOG_INFO("%02x:%02x:%02x:%02x:%02x:%02x",
                         p_adv_report->peer_addr.addr[0],
                         p_adv_report->peer_addr.addr[1],
                         p_adv_report->peer_addr.addr[2],
                         p_adv_report->peer_addr.addr[3],
                         p_adv_report->peer_addr.addr[4],
                         p_adv_report->peer_addr.addr[5]
                         );
            }
        }
        else
        {
            
        }
    
        err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer);
        APP_ERROR_CHECK(err_code);
    }

  • Hello,

    Correct, sd_ble_gap_connect() will connect to the device that has the address you provide. Note that ongoing scanning is not stopped if you don't connect, so sd_ble_gap_scan_start() may return NRF_ERROR_INVALID_STATE.

Related