Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Controlling advertising timing

Using nRF5 SDK v16.0.0 on nRF5232. IDE = SES

When advertising is started, I want to advertise initially for 30 seconds, so I set APP_ADV_DURATION to 3000, and the device will advertise for 30 seconds.  I only allow 1 connection, so when a device connects, the SoftDevice will/should stop advertising. I think that would also cancel the advertising timer?

Given the following sequence of events:

1. Start advertising for 30 seconds
2. 5 seconds elapses
3. Client connects to device
4. Client communicates for 8 seconds
5. Client disconnects

If I start advertising again after the client disconnects, the chip will advertise for 30 seconds.

What I want to do instead is, after the client disconnects, advertise for 17 seconds (the remainder of the advertising time, (30 - 5 - 8 = 17 seconds)) after the client disconnects.

What would be the most ideal way to do this, mind you I want to use BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE.

I assume the solution would be to use a timer and call sd_ble_gap_adv_start and sd_ble_gap_adv_stop as needed, however do I want to use 3000 as a value for APP_ADV_DURATION?  In the case where I stop advertising at the same time the SoftDevice timer stops advertising, would I run into issues (other than sd_ble_gap_adv_stop  returning INVALID_STATE)?  I've run into scenarios where sometimes my device will stop advertising when my manual countdown "remaining seconds to advertise" timer has multiple seconds remaining, which leads me to believe maybe the advertising timer in the soft device doesn't get reset when a device connects, and that timer continues from where it left off the next time I call advertising start--or I have a bug that is prematurely stopping advertising.

So, in pseudocode, my question is, how does the SoftDevice handler the internal advertising timer:

Option 1:

1. Call sd_ble_gap_adv_start to start advertising for 30 seconds
2. 5 seconds elapses
3. Client device connects
4. Internal SoftDevice advertising timer halts at 25 seconds
5. Client device communicates for 8 seconds
6. Client device disconnects
7. Call sd_ble_gap_adv_start, SoftDevice reloads advertising timer with 25 seconds (where it left off)
8. 25 seconds elapses
9. Advertising timer in SoftDevice expires and calls sd_ble_gap_adv_stop to stop advertising

or this way:

Option 2:

1. Call sd_ble_gap_adv_start to start advertising for 30 seconds
2. 5 seconds elapses
3. Client device connects
4. Internal SoftDevice advertising timer halts at 25 seconds
5. Client device communicates for 8 seconds
6. Client device disconnects
7. Call sd_ble_gap_adv_start, SoftDevice reloads advertising timer with 30 seconds (APP_ADV_DURATION)
8. 30 seconds elapses
9. Advertising timer in SoftDevice expires and calls sd_ble_gap_adv_stop to stop advertising

Is the solution this:

1. Set remaining_advertising_count = 30 (do remaining_advertising_count-- every 1 second)
2. Call sd_ble_gap_adv_start to start advertising (for 30 seconds)
3. 5 seconds elapses
4. Client device connects (remaining_advertising_count is now equal to 25)
5. Client device communicates for 8 seconds then disconnects (remaining_advertising_count is now equal to 17)
6. Call sd_ble_gap_adv_start to start advertising (would the internal SoftDevice timer restart at 30 seconds?)
7. 17 seconds elapses, now remaining_advertising_count is equal to 0
8. remaining_advertising_count == 0, so call sd_ble_gap_adv_stop
9. 5 seconds elapses
10. Set remaining_advertising_count = 30 (do remaining_advertising_count-- every 1 second)
11. Call sd_ble_gap_adv_start (at this point, would the internal SoftDevice timer restart at 30?)
12. 30 seconds elapses
13. remaining_advertising_count == 0, call sd_ble_gap_adv_stop, but so would the SoftDevice because advertising timer expired

My questions within there are somewhat a repeat of asking how the internal timer works--I'm wondering if a previous timer value/handle be used on consecutive calls to sd_ble_gap_adv_start

  • Hi

    For your first "preliminary" question. The advertising timeout will reset whenever it stops advertising, so that shouldn't be what stops the advertising when you're waiting for the "remaining advertising" timer, this must be something else stopping your advertisement. Does the device perhaps connect to a central again? Option 2 should be basically what the SoftDevice handler does when "restarting" advertising.

    I think something like what you explain here should be fine. Perhaps if you before restarting advertisements, you calculate the remaining advertising time, and set this to the "new" APP_ADV_DURATION, so it advertises for the remaining amount of time. Then after it times out, you can set the APP_ADV_DURATION back to 30 seconds.

    Best regards,

    Simon

Related