@@ -202,13 +202,19 @@ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t
202202// If we receive a disconnect event then invoke the callback for disconnects (if one is present).
203203// we also want to start advertising again.
204204case ESP_GATTS_DISCONNECT_EVT: {
205- m_connectedCount--; // Decrement the number of connected devices count.
206205if (m_pServerCallbacks != nullptr ) { // If we have callbacks, call now.
207206m_pServerCallbacks->onDisconnect (this );
208207}
209- startAdvertising (); // - do this with some delay from the loop()
210- removePeerDevice (param->disconnect .conn_id , false );
211- break ;
208+ if (m_connId == ESP_GATT_IF_NONE) {
209+ return ;
210+ }
211+
212+ // only decrement if connection is found in map and removed
213+ // sometimes this event triggers w/o a valid connection
214+ if (removePeerDevice (param->disconnect .conn_id , false )) {
215+ m_connectedCount--; // Decrement the number of connected devices count.
216+ }
217+ break ;
212218} // ESP_GATTS_DISCONNECT_EVT
213219
214220
@@ -395,8 +401,8 @@ void BLEServer::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
395401m_connectedServersMap.insert (std::pair<uint16_t , conn_status_t >(conn_id, status));
396402}
397403
398- void BLEServer::removePeerDevice (uint16_t conn_id, bool _client) {
399- m_connectedServersMap.erase (conn_id);
404+ bool BLEServer::removePeerDevice (uint16_t conn_id, bool _client) {
405+ return m_connectedServersMap.erase (conn_id) > 0 ;
400406}
401407/* multi connect support */
402408
0 commit comments