@@ -202,13 +202,19 @@ void BLEServer::handleGATTServerEvent(esp_gatts_cb_event_t event, esp_gatt_if_t
202
202
// If we receive a disconnect event then invoke the callback for disconnects (if one is present).
203
203
// we also want to start advertising again.
204
204
case ESP_GATTS_DISCONNECT_EVT: {
205
- m_connectedCount--; // Decrement the number of connected devices count.
206
205
if (m_pServerCallbacks != nullptr ) { // If we have callbacks, call now.
207
206
m_pServerCallbacks->onDisconnect (this );
208
207
}
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 ;
212
218
} // ESP_GATTS_DISCONNECT_EVT
213
219
214
220
@@ -395,8 +401,8 @@ void BLEServer::addPeerDevice(void* peer, bool _client, uint16_t conn_id) {
395
401
m_connectedServersMap.insert (std::pair<uint16_t , conn_status_t >(conn_id, status));
396
402
}
397
403
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 ;
400
406
}
401
407
/* multi connect support */
402
408
0 commit comments