Disabling the advertising and scanning by a short press of a button and re-enabling it by pressing the same button again.

Hello Team, I hope you will be fine.

We are working on a project where we integrated multiple libraries from different projects. We are using soft devices too.  Now I want to put the device in sleep mode(all the other events should turn off except the RTC) by short pressing the button and returning back to normal operation by pressing again the same button. The problem is that we cannot use a simple system on sleep mode because we have the GPS Data which is 10 samples per second and will wake the device almost all the time and consume a lot of power. Similarly, we cannot use the system off mode as the device gets reset after that which we don't want. So I want to disable all the events like advertising, scanning, GPS etc. manually by short pressing a button and returning back to normal operation by pressing the same button again. I don't know how will I do it like disabling the advertising, scanning, GPS etc. manually and re-enabling by pressing the same button again. Simply I want the device to be OFF (Except the RTC) by pressing a button and ON by pressing the same button again. Any help regarding this will be highly appreciated. 

Kind Regards,

Sami Ul Haq

  • Hello Einar, Thanks for the reply.

    Yes, I am observing behaviour in debugging as you can see in the below attached picture.

    before the short press of a button, the Polar H10 is connected and I can see the Heart Rate in debugging, but when I short press the button, it disconnects the Polar H10 and I can no longer see Heart Rate in debugging. When I long-press the same button and call  scan_init() and scan_start(), the Heart Rate doesn't re-appear in the debug. In the below pictures you can see my disconnect functions.

    As you can see in the above picture(which is for short press) I am calling two disconnect functions one for scanning and the other for advertising. The scanned device(Polar H10) got disconnected and the advertised device(Mobile App) doesn't, even I change the ble_adv_on_disconnect_disabled == true in ble_advertising.c as per your suggestion. Maybe I am not passing the correct parameter in the disconnect function for disconnecting the advertised device, please check it I am using the ble_app_hrs_rscs_relay example.

    The below picture is the code for a long press of the same button where I want to wake the device that is to start advertising, scanning etc and come back to normal functions(wake).

    Kind Regards, 

    Sami Ul Haq

  • Hi Sami,

    Looking at your second code snippet, let me clarify one thing. sd_ble_gap_disconnect() is used to disconnect connections. It is wrong to try to use this to stop advertising or scanning. To stop advertising, you call sd_ble_gap_adv_stop(), and do stop scaning, call nrf_ble_scan_stop(), as you do. So remove the two sd_ble_gap_disconnect() calls you have there.

    However, if you have a connection you want to terminate you should call sd_ble_gap_disconnect() with the connection handle of the connection (which you got from the BLE_GAP_EVT_CONNECTED event). I do not see that anywhere in your code snippet, but that is what you need to do.

  • Sir, I have resolved the issue. Thank you so much for your help.

    Regards,

    Sami Ul Haq

Related