This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Restart the advertising

Dear Sir,

I want to do the following scenario.

Once I power the device it will start advertising.

Now the advertising duration is 2minutes.

I have written a routine based on timer. When it is called it should initiate the advertising again.

How can I  restart an advertising sequence once a current advertising going on?.

I tried the following sequence

(void) sd_ble_gap_adv_stop(&m_advertising.adv_handle);
advertising_start(erase_bonds);

But it shows the error

 WakeUp
<error> app: ERROR 8 [NRF_ERROR_INVALID_STATE] at I:\GK\Rigado\sdk\nRF5_SDK_15.0.0_a53641a\examples\ble_peripheral\Sieva_prod1_wologs\main.c:1325

What am I missing in restarting advertising routine?

In function "ble_advertising_restart_without_whitelist()", I think it follows the same sequence.

But I dont know why it is not working for me.

Please shed some light on this.

with regards,

Geetha

  • Hi.

    The error code is most likely because when you try to stop advertising, you're not advertising. Look at this question for more information about NRF_ERROR_INVALID_STATE and advertising.

    The proper way to start/stop would be:

    //Stop
    err_code = sd_ble_gap_adv_stop(m_adv_handle);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("Stopping advertising");
            
    //Start        
    err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("Starting advertising");

    Look at this question, there is an attached .zip file containing a modified beacon example, where start/stop advertising is implemented as a handle of pressing button 1 (in void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action))

    - Andreas

  • Hi,

    Thank you very much for your response.

    My scenario is as follows.

    I am using 2 timers.

    First timer will wake up for each 5 mins and second one wake up after the first 1 minute.

    So when I start the device , it advertises. And initial 1 minute I will do a connect-disconnect method to get the adddresses.

    Now once I disconnect the device it will start advertising since the function "ble_advertising_start()"  is getting called inside "on_disconnected()" function.

    If I want to stop advertising from the main.c file  how can I do this?..

    how can I get the advertising handle?

    Which function I should call from the main function to stop the current advertising instance and to start a new one?

    I tried your scenario, but it shows the same problem

    Thanking you

  • Hi.

     

    AndreasF said:
    Look at this question, there is an attached .zip file containing a modified beacon example, where start/stop advertising is implemented as a handle of pressing button 1 (in void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action))

     

    Did you look at this? It's pretty straight forward shown how to use the two API's I posted in my previous reply.

    The handle is initialized in the top of main.c

    static uint8_t              m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an advertising set. */

    And the pin handler starts and stops advertising.

    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        ret_code_t err_code;
    
        nrf_drv_gpiote_out_toggle(PIN_OUT);
       
    
        if(is_advertising){ 
            err_code = sd_ble_gap_adv_stop(m_adv_handle);
            APP_ERROR_CHECK(err_code);
            NRF_LOG_INFO("Stopping advertising");
            is_advertising = false;
        } else{
            err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
            APP_ERROR_CHECK(err_code);
            NRF_LOG_INFO("Starting advertising");
            is_advertising = true;
        }
    }
    

    You have probably defined &m_advertising as your advertising handle.

    - Andreas

  • Hi Andeas,

    Thank you very much for your reply.

    That means I am not supposed to use the function "ble_advertising_start()" to start advertising?

     And instead of that should I use "sd_ble_gap_adv_start()" directly?

    Inside this function  "ble_advertising_start()"   it uses "sd_ble_gap_adv_start()" and some more things extra.

    Please shed some light.

  • Hi.

    ble_advertising_start() is an API call to start advertising using the Advertising Module. At the bottom of the module, advertising is started by sd_ble_gap_adv_start() (line 657 in ble_advertising.c) If you don't need to do some of the "extra things" you refer to before you start advertising, you don't need to use ble_advertising_start().

    If you only need to start or stop advertising you can safely use only sd_ble_gap_adv_start() and sd_ble_gap_adv_stop().

    - Andreas

Related