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

NRF_ERROR_BUSY returned, re-starting BLE beacon advertising

Hey guys,

Working with the simple be_app_beacon example on nRF51. I’d like to be able to stop and start advertising. But after stopping advertising, I’m unable to start it again. On the call to start advertising I’m getting an err_code of NRF_ERROR_BUSY.

I’m guessing the structure of the code here isn’t allowing the BLE stack to process the previous call to stop the advertising, before I try to start it again. Is there a really simple, nice way to fix this? It's just a basic NON connectable beacon. See simplified code below in my main loop.

Sorry for the newb question. Hoping to keep the code really clean. Much appreciated.

Thanks, Brian

main() {

ble_stack_init();

adv_beacon_init();

advertising_start();

delay_flag = FALSE;

app_timer_start(…); // sets delay_flag to FALSE after 1 second

while(delay_flag) power_manage();

sd_ble_gap_adv_stop();

//do stuff here… not BLE related

err_code = sd_ble_gap_adv_start(); //!!! here’s where err_code == NRF_ERROR_BUSY

//do more stuff, then call sd_power_system_off();

}

Parents
  • #define APP_CFG_NON_CONN_ADV_TIMEOUT  1
    #define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(1000, UNIT_0_625_MS)
    

    This will send one advertisement per second for 1 second.

    #define APP_CFG_NON_CONN_ADV_TIMEOUT  0
    #define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(1000, UNIT_0_625_MS)
    

    This will send one advertisement per second forever, or until you call sd_ble_gap_adv_stop().

    If you always want to stop the advertisements manually you can set the timeout to 0.

Reply
  • #define APP_CFG_NON_CONN_ADV_TIMEOUT  1
    #define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(1000, UNIT_0_625_MS)
    

    This will send one advertisement per second for 1 second.

    #define APP_CFG_NON_CONN_ADV_TIMEOUT  0
    #define NON_CONNECTABLE_ADV_INTERVAL    MSEC_TO_UNITS(1000, UNIT_0_625_MS)
    

    This will send one advertisement per second forever, or until you call sd_ble_gap_adv_stop().

    If you always want to stop the advertisements manually you can set the timeout to 0.

Children
  • Yup. Got that. What I'm finding in SDK9-2 is that if the timeout is 1 second. And I wait more than 1 second after starting advertising, then trying to restart advertising after the timeout period I get the NRF_BUSY error.

    With timeout of 1 second, and a delay of only 500ms, let's say, then stoping and starting works ok. Seems strange... but no big deal. Now I'm just setting the timeout to 0, and manually starting and stopping and it's working ok.

  • Also, if I really only want to send on advertisement per second, how quickly should I stop advertising after starting it? ~999ms? Assuming an interval of 1000ms. In other words, after starting advertising, how quickly is that first advertisement sent? Thanks!

  • If you are not doing anything else, for example scanning or something else (I don't know what SoftDevice you are using) the advertisement will be sent pretty quickly, but exactly how fast I don't know. 100 ms should be fine. You can find out by using Radio Notifications or a sniffer.

    You can also skip the timer. Set the timeout to 1 second, and the interval to for example 1100 ms. Then when you get BLE_GAP_EVT_TIMEOUT you start advertising again, or not.

    I also want to point out that when I say one advertisement, I mean three advertising packet on each of the three advertising channels.

  • Thanks Petter... really appreciated.

    One last question. I'm using the template/example provided in SDK 9-2, ble_app_beacon, and its not clear to me where to initialize advertising with a callback to an event handler to catch BLE_GAP_EVT_TIMEOUT as you point out. Is it possible?

    Thanks, Brian

    You can also skip the timer. Set the timeout to 1 second, and the interval to for example 1100 ms. Then when you get BLE_GAP_EVT_TIMEOUT you start advertising again, or not.

  • In ble_stack_init() you need to add:

    // Register with the SoftDevice handler module for BLE events.
    err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
    APP_ERROR_CHECK(err_code);
    

    And you need to define the event handler:

    static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
    {
        if((p_ble_evt->header.evt_id == BLE_GAP_EVT_TIMEOUT) && (p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_ADVERTISING))
        {
             //Start advertising or not
        }
    }
    
Related