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

How to stop advertising ?

Hi,

I am testing **ble_app_uart example in the path of nRF51-SDK\nRF51_SDK_9.0.0_2e23562\examples\ble_peripheral\ble_app_uart\pca10028\s110 \arm5_no_packs\ble_app_uart_s100_pca10028.

I want to start and stop advertising manually on a certain condition.

ble_advertising_start(BLE_ADV_MODE_FAST) works for advertising . But I can't stop it.

I tried several ways, still in trouble.

I need help.

Thanks.

  • I believe you can use a combination of ble_advertising_start(BLE_ADV_MODE_IDLE); and sd_ble_gap_adv_stop(); to stop the advertisement and keep it stopped. Without the first call the library might restart advertisement on ble/sys events if you have them dispatched. Without the latter call the library won't stop advertising on its own.

    It's rather odd that the library doesn't provide an explicit way to stop advertising. Maybe a bug/lack of a feature or maybe I don't understand the use case. :)

  • Hi,

    I think the general idea is to let APP_ADV_TIMEOUT_IN_SECONDS decide how long you advertise for, the advertisement is cancelled if the device gets connected to.

  • Ah yes the timeouts. I'm too used to infinite advertisement and manual control. :) I guess the library isn't really intended for general use.

  • Thanks

    As atune answers, sd_ble_gap_adv_stop() works for stopping it. However sometimes sd_ble_gap_adv_stop() does not works as I intended by other events.

    When put the function in the nus_data_handler(ble_nus_t * p_nus, uint8_t * mbda_data, uint16_t length), it does not work. So I make a flag in the hadler for an condition, and then check the flag in main(){ } and applied sd_ble_gap_adv_stop() as following.

    main {
    ~~~
      for (;;)
        {
    	power_manage();
    	
           if (CONN_FLAG == true)
            { 
    	     sd_ble_gap_adv_stop();
    	}			
        }
    }
    

    I am not sure whether I am doing right.

    APP_ADV_TIMEOUT_IN_SECONDS is not applicable to my case because stopping period of advertising is not constant. When a correct value is received by NUS Event, advertising should stop. If I can make APP_ADV_TIMEOUT_IN_SECONDS a static variable, it may work. I think.

    If is there better way, please let me know. Thanks.

  • Does it make sense for your application to stop advertising within the ble_nus_data handler? I expect that there won't be any advertising going on at that point anyway, since the device is already connected.

    SoftDevice API calls run at interrupt priority level 2, this means that you cannot call them from higher priority interrupt, ie. application high, which runs at priority 1.

Related