@@ -109,7 +109,16 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
109
109
return false ;
110
110
}
111
111
112
- m_semaphoreRegEvt.wait (" connect" );
112
+ uint32_t rc = m_semaphoreRegEvt.wait (" connect" );
113
+
114
+ if (rc != ESP_GATT_OK) {
115
+ // fixes ESP_GATT_NO_RESOURCES error mostly
116
+ log_e (" esp_ble_gattc_app_register_error: rc=%d" , rc);
117
+ BLEDevice::removePeerDevice (m_appId, true );
118
+ // not sure if this is needed here
119
+ // esp_ble_gattc_app_unregister(m_gattc_if);
120
+ return false ;
121
+ }
113
122
114
123
m_peerAddress = address;
115
124
@@ -127,7 +136,12 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) {
127
136
return false ;
128
137
}
129
138
130
- uint32_t rc = m_semaphoreOpenEvt.wait (" connect" ); // Wait for the connection to complete.
139
+ rc = m_semaphoreOpenEvt.wait (" connect" ); // Wait for the connection to complete.
140
+ // check the status of the connection and cleanup in case of failure
141
+ if (rc != ESP_GATT_OK) {
142
+ BLEDevice::removePeerDevice (m_appId, true );
143
+ esp_ble_gattc_app_unregister (m_gattc_if);
144
+ }
131
145
log_v (" << connect(), rc=%d" , rc==ESP_GATT_OK);
132
146
return rc == ESP_GATT_OK;
133
147
} // connect
@@ -159,6 +173,11 @@ void BLEClient::gattClientEventHandler(
159
173
log_d (" gattClientEventHandler [esp_gatt_if: %d] ... %s" ,
160
174
gattc_if, BLEUtils::gattClientEventTypeToString (event).c_str ());
161
175
176
+ // it is possible to receive events from other connections while waiting for registration
177
+ if (m_gattc_if == ESP_GATT_IF_NONE && event != ESP_GATTC_REG_EVT) {
178
+ return ;
179
+ }
180
+
162
181
// Execute handler code based on the type of event received.
163
182
switch (event) {
164
183
@@ -183,15 +202,16 @@ void BLEClient::gattClientEventHandler(
183
202
if (evtParam->disconnect .conn_id != getConnId ()) break ;
184
203
// If we receive a disconnect event, set the class flag that indicates that we are
185
204
// no longer connected.
186
- if (m_isConnected && m_pClientCallbacks != nullptr ) {
187
- m_pClientCallbacks->onDisconnect (this );
188
- }
205
+ bool m_wasConnected = m_isConnected;
189
206
m_isConnected = false ;
190
207
esp_ble_gattc_app_unregister (m_gattc_if);
191
208
m_semaphoreOpenEvt.give (ESP_GATT_IF_NONE);
192
209
m_semaphoreRssiCmplEvt.give ();
193
210
m_semaphoreSearchCmplEvt.give (1 );
194
211
BLEDevice::removePeerDevice (m_appId, true );
212
+ if (m_wasConnected && m_pClientCallbacks != nullptr ) {
213
+ m_pClientCallbacks->onDisconnect (this );
214
+ }
195
215
break ;
196
216
} // ESP_GATTC_DISCONNECT_EVT
197
217
@@ -227,7 +247,8 @@ void BLEClient::gattClientEventHandler(
227
247
//
228
248
case ESP_GATTC_REG_EVT: {
229
249
m_gattc_if = gattc_if;
230
- m_semaphoreRegEvt.give ();
250
+ // pass on the registration status result, in case of failure
251
+ m_semaphoreRegEvt.give (evtParam->reg .status );
231
252
break ;
232
253
} // ESP_GATTC_REG_EVT
233
254
0 commit comments