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

sd_ble_gap_adv_stop()

I have a beacon type running firmware. My system should start advertising when I press button A. and then stop when I press button B. So here is the scenerio:

Scene 1:Press button A(ok), press button B(error/restart) Scene 2:Press button A(ok), press button B(ok), press button A(error/restart)

I read a lot of these in the forums but cannot find how to properly resolved the issue. Like, stopping the advertisment when it is not advertising, but, how should I know if its in advertising state or not.

Is there any other way to implement this or other function to call. Thank you very much.

  • Well if you write your FW you should know in what state your application is. You can have stupid bool variable saying "advertising ON/OFF". Because it's application FW which is implementing GAP profiles it's kind of logical it must track its state. Anyway even for lazy developers there is a option: simply get status code returned by Soft Device and if it's NRF_ERROR_INVALID_STATE then you probably weren't advertising and you can try to ignore it (but it's not advised to ignore API documentation so please read it;) Indeed you can get this error even in different situations (like when SD isn't properly initialized/enabled) but that's kind of obvious... and if you ignore keeping track about internal states of your app it's good chance you will have bigger problems and dead-locks then this;)

  • as you can see in my example right. I don't need a stupid bool variable to monitor advertising. I press A button, check NRF connect if advertising(adv=ok), then I press button B to call sd_ble_gap_adv_stop(). But the function never returns any, from there it will restart my mcu.

  • There is no code example, just some loosy sequence which doesn't indicate that sd_ble_gap_adv_stop() function really hangs. if it would be true then you couldn't perform restart from application (because it would be hanging;). So what is most probably happening (cannot be certain unless you give us some more detailed debug trace) is that SD call simply returns error (because you call it out of context = your application is responsible to track GAP context of each "slot" and if you try to stop something which is not running then no surprise you get error status back) and you decide to treat it by assert. My advice stays: either start tracking GAP context properly in your app or stay lazy but then don't treat this error as hard fault.

Related