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

Switching advertising type

I have seen that the recommended method for switching between connectable and non-connectable advertising is:

    sd_ble_gap_adv_stop()
    ...change advertising structures as needed...
    sd_ble_gap_adv_start()

My app is doing this, and it works. But using sd_ble_gap_adv_start() instead of ble_advertising_start() causes my app to no longer get events to on_adv_evt(). When I try to change to using ble_advertising_start() instead, I get crashes on restarting (works fine starting the first time).

So is there a way to switch between connectable and non-connectable advertising while still getting advertising events?

Parents
  • Hello,

    Can you try the solution from this post?

    I managed to run sd_ble_gap_adv_start(&adv_params, NULL)     (note that the second input is ignored when you use BLE_GAP_ADV_TYPE_ADV_NONCONN_IND)

     

    I tried the following:

    ret_code_t non_conn_advertising(void)
    {
        NRF_LOG_INFO("non_conn_advertising()");
        ret_code_t err_code;
        
        ble_gap_adv_params_t adv_params;
        memset(&adv_params, 0, sizeof(adv_params));
        adv_params.type         = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
        adv_params.p_peer_addr  = NULL;
        adv_params.fp           = BLE_GAP_ADV_FP_ANY;
        adv_params.interval     = NON_CONNECTABLE_ADV_INTERVAL;
        adv_params.timeout      = NON_CONNECTABLE_ADV_TIMEOUT;
        
        err_code = sd_ble_gap_adv_start(&adv_params, NULL);
        NRF_LOG_INFO("err_code = %d", err_code);
        APP_ERROR_CHECK(err_code);
        
        return NRF_SUCCESS;
    }

     

    And then I added err_code = non_conn_advertising(); within the BLE_GAP_EVT_CONNECTED event in ble_evt_handler(...)

     

    I have not tried to sniff the advertising yet, but I saw that I could still find the device from nRF Connect for desktop while I was connected with my phone. (The rssi was still changing, but of course, I could not connect to it).

     

    Also note that NON_CONNECTABLE_ADV_INTERVAL must be larger than 160 (=100ms in units of 0.625).

     

    Best regards,

    Edvin

     

Reply
  • Hello,

    Can you try the solution from this post?

    I managed to run sd_ble_gap_adv_start(&adv_params, NULL)     (note that the second input is ignored when you use BLE_GAP_ADV_TYPE_ADV_NONCONN_IND)

     

    I tried the following:

    ret_code_t non_conn_advertising(void)
    {
        NRF_LOG_INFO("non_conn_advertising()");
        ret_code_t err_code;
        
        ble_gap_adv_params_t adv_params;
        memset(&adv_params, 0, sizeof(adv_params));
        adv_params.type         = BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
        adv_params.p_peer_addr  = NULL;
        adv_params.fp           = BLE_GAP_ADV_FP_ANY;
        adv_params.interval     = NON_CONNECTABLE_ADV_INTERVAL;
        adv_params.timeout      = NON_CONNECTABLE_ADV_TIMEOUT;
        
        err_code = sd_ble_gap_adv_start(&adv_params, NULL);
        NRF_LOG_INFO("err_code = %d", err_code);
        APP_ERROR_CHECK(err_code);
        
        return NRF_SUCCESS;
    }

     

    And then I added err_code = non_conn_advertising(); within the BLE_GAP_EVT_CONNECTED event in ble_evt_handler(...)

     

    I have not tried to sniff the advertising yet, but I saw that I could still find the device from nRF Connect for desktop while I was connected with my phone. (The rssi was still changing, but of course, I could not connect to it).

     

    Also note that NON_CONNECTABLE_ADV_INTERVAL must be larger than 160 (=100ms in units of 0.625).

     

    Best regards,

    Edvin

     

Children
No Data
Related