@@ -295,9 +295,9 @@ bool BLEManagerImpl::CloseConnection(BLE_CONNECTION_OBJECT conId)
295295 // Release the associated connection state record.
296296 ReleaseConnectionState (conId);
297297
298- // Arrange to re-enable connectable advertising in case it was disabled due to the
299- // maximum connection limit being reached.
300- ClearFlag (mFlags , kFlag_Advertising );
298+ // Force a refresh of the advertising state.
299+ SetFlag ( mFlags , kFlag_AdvertisingRefreshNeeded );
300+ ClearFlag (mFlags , kFlag_AdvertisingConfigured );
301301 PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
302302
303303 return (err == WEAVE_NO_ERROR);
@@ -439,24 +439,28 @@ void BLEManagerImpl::DriveBLEState(void)
439439 ExitNow ();
440440 }
441441
442- // Start advertising if needed ...
442+ // If the application has enabled WoBLE and BLE advertising ...
443443 if (mServiceMode == ConnectivityManager::kWoBLEServiceMode_Enabled && GetFlag (mFlags , kFlag_AdvertisingEnabled )
444444#if WEAVE_DEVICE_CONFIG_WOBLE_SINGLE_CONNECTION
445445 // and no connections are active...
446446 && (_NumConnections () == 0 )
447447#endif
448448 )
449449 {
450- // Configure advertising data if needed.
451- if (!GetFlag (mFlags , kFlag_AdvertisingConfigured ))
452- {
453- err = ConfigureAdvertisingData ();
454- ExitNow ();
455- }
456-
457- // Start advertising if needed.
450+ // Start/re-start advertising if not already advertising, or if the advertising state of the
451+ // ESP BLE layer needs to be refreshed.
458452 if (!GetFlag (mFlags , kFlag_Advertising ) || GetFlag (mFlags , kFlag_AdvertisingRefreshNeeded ))
459453 {
454+ // Configure advertising data if it hasn't been done yet. This is an asynchronous step which
455+ // must complete before advertising can be started. When that happens, this method will
456+ // be called again, and execution will proceed to the code below.
457+ if (!GetFlag (mFlags , kFlag_AdvertisingConfigured ))
458+ {
459+ err = ConfigureAdvertisingData ();
460+ ExitNow ();
461+ }
462+
463+ // Start advertising. This is also an asynchronous step.
460464 err = StartAdvertising ();
461465 ExitNow ();
462466 }
@@ -845,12 +849,10 @@ void BLEManagerImpl::HandleGATTCommEvent(esp_gatts_cb_event_t event, esp_gatt_if
845849 // Allocate a connection state record for the new connection.
846850 GetConnectionState (param->mtu .conn_id , true );
847851
848- // Receiving a connection stops the advertising processes. So record that the advertising state
849- // in the ESP BLE layer needs to be refreshed. If advertising should to be re-enabled, this will
850- // be handled in DriveBLEState() the next time it runs.
852+ // Receiving a connection stops the advertising processes. So force a refresh of the advertising
853+ // state.
851854 SetFlag (mFlags , kFlag_AdvertisingRefreshNeeded );
852-
853- // Re-evaluate the advertising state in light of the new connection.
855+ ClearFlag (mFlags , kFlag_AdvertisingConfigured );
854856 PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
855857
856858 break ;
@@ -1100,9 +1102,9 @@ void BLEManagerImpl::HandleDisconnect(esp_ble_gatts_cb_param_t * param)
11001102 }
11011103 PlatformMgr ().PostEvent (&event);
11021104
1103- // Arrange to re-enable connectable advertising in case it was disabled due to the
1104- // maximum connection limit being reached.
1105- ClearFlag (mFlags , kFlag_Advertising );
1105+ // Force a refresh of the advertising state.
1106+ SetFlag ( mFlags , kFlag_AdvertisingRefreshNeeded );
1107+ ClearFlag (mFlags , kFlag_AdvertisingConfigured );
11061108 PlatformMgr ().ScheduleWork (DriveBLEState, 0 );
11071109 }
11081110}
@@ -1220,6 +1222,7 @@ void BLEManagerImpl::HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb
12201222 }
12211223
12221224 ClearFlag (sInstance .mFlags , kFlag_ControlOpInProgress );
1225+ ClearFlag (sInstance .mFlags , kFlag_AdvertisingRefreshNeeded );
12231226
12241227 // Transition to the Advertising state...
12251228 if (!GetFlag (sInstance .mFlags , kFlag_Advertising ))
@@ -1248,6 +1251,7 @@ void BLEManagerImpl::HandleGAPEvent(esp_gap_ble_cb_event_t event, esp_ble_gap_cb
12481251 }
12491252
12501253 ClearFlag (sInstance .mFlags , kFlag_ControlOpInProgress );
1254+ ClearFlag (sInstance .mFlags , kFlag_AdvertisingRefreshNeeded );
12511255
12521256 // Transition to the not Advertising state...
12531257 if (GetFlag (sInstance .mFlags , kFlag_Advertising ))
0 commit comments