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

sd_ble_gap_connect with not-scanned address as a parameter

Hi,

I'm evaluating the Raytac MDBT50Q devkit, using nRF52840-DK to program it. I'm testing the system with SDK project nRF5_SDK_15.3.0_59ac345\examples\ble_central\ble_app_uart_c

Scanning and connecting to advertising devices is working, but when I tested modifying the application so, that the UUID-address in sd_ble_gap_connect() parameter differs from the one scanned, the system seems to be hanging.

The code for making a connection is following:

static void on_adv_report(ble_gap_evt_adv_report_t const * p_adv_report)
{

****

        // If the address is correct, stop scanning and initiate a connection with the peripheral device.
        nrf_ble_scan_stop();

        static ble_gap_addr_t addr; // Have a local address structure that can be modified

        // Copy the contents from the SCAN info
        addr.addr_id_peer = p_adv_report->peer_addr.addr_id_peer;
        addr.addr_type    = p_adv_report->peer_addr.addr_type;
        for (i=0;i<BLE_GAP_ADDR_LEN;++i)
        {
            addr.addr[i] = p_adv_report->peer_addr.addr[i];
        }

        addr.addr[0] = 0x07; // Modify the address, which originally is 0F0AC761FED6

        err_code = sd_ble_gap_connect(&addr,                   //&p_adv_report->peer_addr,
                                      &m_scan.scan_params,
                                      &m_scan.conn_params,
                                      APP_BLE_CONN_CFG_TAG);
        APP_ERROR_CHECK(err_code);

        if (NRF_SUCCESS == err_code)     printf("Connecting\r\n");
}

The sd_ble_gap_connect() call returns success, since "Connecting" is printed out. Timers are working but no connection/scan related events occurs afterwards.

Reason for making this kind of test is the fact that the module is connected to the host via UART and it can't be guaranteed that an address used for connection is always correct.

Questions:

* can the sd_ble_gap_connect() call be made at any point in the code, or must is always be made from inside the on_adv_report() event handler?

* what is the status of the connection manager in this situation? How can we detect the flaw and fix it - maybe the nrf_ble_scan_start() must be called?

* must the address given to sd_ble_gap_connect() always be just scanned?

Best regards, Jukka Lamminmäki

Parents
  • Hi,

    * can the sd_ble_gap_connect() call be made at any point in the code, or must is always be made from inside the on_adv_report() event handler?

    It does not need to be inside a on_adv_report(). You can call it where you want.

    How can we detect the flaw and fix it

    You can change the address you want to connect to. You dont need to call nrf_ble_scan_start().

    If a wrong address is a plausible scenario in your use-case, you might want to call sd_ble_gap_connect() with a timeout in ble_gap_scan_params_t. You will then get a BLE_GAP_EVT_TIMEOUT event if you were not able to connect.

    must the address given to sd_ble_gap_connect() always be just scanned?

    No. But the device you want to connect to need to be advertising.

  • Sigurd, thanx for an answer,

    Shouldn't it actually be the ble_gap_conn_params_t parameter for setting the connection timeout? At least the name sound more suitable.

    What is actually the field to be set?

    If the parameter is ble_gap_conn_params_t , reading the header file documentation one would guess that the field to be set is ble_gap_conn_params_t->conn_sup_timeout, which seems to be by default NRF_BLE_SCAN_SUPERVISION_TIMEOUT, which is 40 seconds.

    If the parameter to be modified is truly ble_gap_scan_params_t (which by name is a bit confusing), what would be the correct field to be set there?

    However my project never enters ble_evt_handler() after calling the sd_ble_gap_connect(), which seems to be indicating that there's no timeout set, or that the timeout is very long.

    When entering the function, connection parameters are:

    m_scan.conn_params.conn_sup_timeout = 400
    m_scan.conn_params.max_conn_interval = 24
    m_scan.conn_params.min_conn_interval  = 6
    m_scan.conn_params.slave_latency  = 0

    Rgrds, Jukka

  • Jukka Lamminmaki said:
    If the parameter to be modified is truly ble_gap_scan_params_t

     It is. You are still scanning, the connection has not been established. It's the timeout field, see this link.

    Jukka Lamminmaki said:
    which is 40 seconds.

     It's in 10ms units, so it's 400 * 10ms = 4000ms = 4 seconds. The connection supervision timeout can be considered as the maximum time between two received valid data packets before a connection is considered lost.

  • Thank You again Sigurd, and sorry for me questioning your original answer.

    Now I got it running. Adding  the nrf_ble_scan_start() call into the BLE_GAP_EVT_TIMEOUT case restarted the scanning again.

    As you can maybe figure out from my questions, we are just starting to evaluate the SDK and module.

    One problem is that there's so many features and so much documentation available, that finding the very basics is quite hard. And field document saying "Scan timeout in 10 ms units." doesn't actually tell anything to the developer who doesn't quite understand the whole process.

    Is there a figure somewhere illustrating the processes going on in scanning and connecting phases, showing which events may be triggered in which cases?

    Best regards, Jukka

  • Now I got it running.

    Great!

    Is there a figure somewhere illustrating the processes going on in scanning and connecting phases, showing which events may be triggered in which cases?

    The documentation for the function sd_ble_gap_connect() can be found at this link.It shows you the events that can be generated, and the relevant Message Sequence Charts. E.g. this one.

Reply Children
No Data
Related