@@ -81,6 +81,8 @@ BLEClient::~BLEClient() {
8181 clearServices ();
8282 esp_ble_gattc_app_unregister (m_gattc_if);
8383 BLEDevice::removePeerDevice (m_appId, true );
84+ if (m_deleteCallbacks)
85+ delete m_pClientCallbacks;
8486
8587} // ~BLEClient
8688
@@ -171,15 +173,14 @@ void BLEClient::gattClientEventHandler(
171173 switch (event) {
172174
173175 case ESP_GATTC_SRVC_CHG_EVT:
174- if (getConnId () != evtParam-> search_res . conn_id )
176+ if (m_gattc_if != gattc_if )
175177 break ;
176178
177179 ESP_LOGI (LOG_TAG, " SERVICE CHANGED" );
178180 break ;
179181
180- case ESP_GATTC_CLOSE_EVT: {
182+ case ESP_GATTC_CLOSE_EVT:
181183 break ;
182- }
183184
184185 //
185186 // ESP_GATTC_DISCONNECT_EVT
@@ -189,8 +190,8 @@ void BLEClient::gattClientEventHandler(
189190 // - uint16_t conn_id
190191 // - esp_bd_addr_t remote_bda
191192 case ESP_GATTC_DISCONNECT_EVT: {
192- ESP_LOGE (__func__, " disconnect event, conn_id : %d" , evtParam->disconnect .conn_id );
193- if (getConnId () != evtParam-> disconnect . conn_id )
193+ ESP_LOGE (__func__, " disconnect event, reason : %d, connId: %d, my connId: %d, my IF: %d, gattc_if: %d " , ( int ) evtParam->disconnect .reason , evtParam-> disconnect . conn_id , getConnId (), getGattcIf (), gattc_if );
194+ if (m_gattc_if != gattc_if )
194195 break ;
195196 m_semaphoreOpenEvt.give (evtParam->disconnect .reason );
196197 if (!m_isConnected)
@@ -214,7 +215,7 @@ void BLEClient::gattClientEventHandler(
214215 // - esp_bd_addr_t remote_bda
215216 //
216217 case ESP_GATTC_OPEN_EVT: {
217- if (getConnId () != ESP_GATT_IF_NONE )
218+ if (m_gattc_if != gattc_if )
218219 break ;
219220 m_conn_id = evtParam->open .conn_id ;
220221 if (m_pClientCallbacks != nullptr ) {
@@ -240,7 +241,6 @@ void BLEClient::gattClientEventHandler(
240241 if (m_appId == evtParam->reg .app_id ){
241242 ESP_LOGI (__func__, " register app id: %d, %d, gattc_if: %d" , m_appId, evtParam->reg .app_id , gattc_if);
242243 m_gattc_if = gattc_if;
243- m_appId = evtParam->reg .app_id ;
244244 m_semaphoreRegEvt.give ();
245245 }
246246 break ;
@@ -255,8 +255,9 @@ void BLEClient::gattClientEventHandler(
255255 break ;
256256
257257 case ESP_GATTC_CONNECT_EVT: {
258- if (evtParam-> connect . conn_id != getConnId () )
258+ if (m_gattc_if != gattc_if )
259259 break ;
260+ m_conn_id = evtParam->connect .conn_id ;
260261 BLEDevice::updatePeerDevice (this , true , m_gattc_if);
261262 esp_err_t errRc = esp_ble_gattc_send_mtu_req (gattc_if, evtParam->connect .conn_id );
262263 if (errRc != ESP_OK) {
@@ -278,7 +279,7 @@ void BLEClient::gattClientEventHandler(
278279 // - uint16_t conn_id
279280 //
280281 case ESP_GATTC_SEARCH_CMPL_EVT: {
281- if (evtParam-> search_cmpl . conn_id != getConnId () )
282+ if (m_gattc_if != gattc_if )
282283 break ;
283284 if (evtParam->search_cmpl .status != ESP_GATT_OK){
284285 ESP_LOGE (LOG_TAG, " search service failed, error status = %x" , evtParam->search_cmpl .status );
@@ -308,8 +309,9 @@ void BLEClient::gattClientEventHandler(
308309 // - esp_gatt_id_t srvc_id
309310 //
310311 case ESP_GATTC_SEARCH_RES_EVT: {
311- if (getConnId () != evtParam-> search_res . conn_id )
312+ if (m_gattc_if != gattc_if )
312313 break ;
314+
313315 BLEUUID uuid = BLEUUID (evtParam->search_res .srvc_id );
314316 BLERemoteService* pRemoteService = new BLERemoteService (
315317 evtParam->search_res .srvc_id ,
@@ -515,8 +517,9 @@ bool BLEClient::isConnected() {
515517/* *
516518 * @brief Set the callbacks that will be invoked.
517519 */
518- void BLEClient::setClientCallbacks (BLEClientCallbacks* pClientCallbacks) {
520+ void BLEClient::setClientCallbacks (BLEClientCallbacks* pClientCallbacks, bool deleteCallbacks ) {
519521 m_pClientCallbacks = pClientCallbacks;
522+ m_deleteCallbacks = deleteCallbacks;
520523} // setClientCallbacks
521524
522525
0 commit comments