@@ -333,6 +333,20 @@ class GapDemo : private mbed::NonCopyable<GapDemo>, public ble::Gap::EventHandle
333333 if (event.isConnected ()) {
334334 printf (" Stopped advertising early due to connection\r\n " );
335335 }
336+
337+ #if BLE_FEATURE_EXTENDED_ADVERTISING
338+ /* we were waiting for it to stop before destroying it and starting scanning */
339+ ble_error_t error = _gap.destroyAdvertisingSet (_extended_adv_handle);
340+ if (error) {
341+ print_error (error, " Error caused by Gap::destroyAdvertisingSet" );
342+ }
343+
344+ _extended_adv_handle = ble::INVALID_ADVERTISING_HANDLE;
345+
346+ _is_in_scanning_phase = true ;
347+
348+ _event_queue.call_in (delay, [this ]{ scan (); });
349+ #endif // BLE_FEATURE_EXTENDED_ADVERTISING
336350 }
337351
338352 void onAdvertisingStart (const ble::AdvertisingStartEvent &event) override
@@ -499,19 +513,13 @@ class GapDemo : private mbed::NonCopyable<GapDemo>, public ble::Gap::EventHandle
499513 print_error (error, " Error caused by Gap::stopAdvertising" );
500514 }
501515 }
502-
503- ble_error_t error = _gap.destroyAdvertisingSet (_extended_adv_handle);
504- if (error) {
505- print_error (error, " Error caused by Gap::destroyAdvertisingSet" );
506- }
507-
508- _extended_adv_handle = ble::INVALID_ADVERTISING_HANDLE;
509516 }
510- # endif // BLE_FEATURE_EXTENDED_ADVERTISING
511-
517+ /* we have to wait before we destroy it until it's stopped */
518+ # else
512519 _is_in_scanning_phase = true ;
513520
514521 _event_queue.call_in (delay, [this ]{ scan (); });
522+ #endif // BLE_FEATURE_EXTENDED_ADVERTISING
515523 }
516524
517525 /* * print some information about our radio activity */
0 commit comments