diff --git a/libraries/BLE/src/BLEClient.cpp b/libraries/BLE/src/BLEClient.cpp index db7a588dd168a2e9270e323def1b3f3fdf7fb771..c056f2556560a6352781b712c0be77188c938f0e 100644 --- a/libraries/BLE/src/BLEClient.cpp +++ b/libraries/BLE/src/BLEClient.cpp @@ -110,7 +110,17 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) { return false; } - m_semaphoreRegEvt.wait("connect"); + uint32_t rc = m_semaphoreRegEvt.wait("connect"); + + if (rc != ESP_GATT_OK) { + // fixes ESP_GATT_NO_RESOURCES error mostly + log_e("esp_ble_gattc_app_register_error: rc=%d", rc); + BLEDevice::removePeerDevice(m_appId, true); + // not sure if this is needed here + // esp_ble_gattc_app_unregister(m_gattc_if); + // m_gattc_if = ESP_GATT_IF_NONE; + return false; + } m_peerAddress = address; @@ -128,7 +138,13 @@ bool BLEClient::connect(BLEAddress address, esp_ble_addr_type_t type) { return false; } - uint32_t rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete. + rc = m_semaphoreOpenEvt.wait("connect"); // Wait for the connection to complete. + // check the status of the connection and cleanup in case of failure + if (rc != ESP_GATT_OK) { + BLEDevice::removePeerDevice(m_appId, true); + esp_ble_gattc_app_unregister(m_gattc_if); + m_gattc_if = ESP_GATT_IF_NONE; + } log_v("<< connect(), rc=%d", rc==ESP_GATT_OK); return rc == ESP_GATT_OK; } // connect @@ -160,6 +176,11 @@ void BLEClient::gattClientEventHandler( log_d("gattClientEventHandler [esp_gatt_if: %d] ... %s", gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str()); + // it is possible to receive events from other connections while waiting for registration + if (m_gattc_if == ESP_GATT_IF_NONE && event != ESP_GATTC_REG_EVT) { + return; + } + // Execute handler code based on the type of event received. switch(event) { @@ -184,15 +205,17 @@ void BLEClient::gattClientEventHandler( if (evtParam->disconnect.conn_id != getConnId()) break; // If we receive a disconnect event, set the class flag that indicates that we are // no longer connected. - if (m_isConnected && m_pClientCallbacks != nullptr) { - m_pClientCallbacks->onDisconnect(this); - } + bool m_wasConnected = m_isConnected; m_isConnected = false; esp_ble_gattc_app_unregister(m_gattc_if); + m_gattc_if = ESP_GATT_IF_NONE; m_semaphoreOpenEvt.give(ESP_GATT_IF_NONE); m_semaphoreRssiCmplEvt.give(); m_semaphoreSearchCmplEvt.give(1); BLEDevice::removePeerDevice(m_appId, true); + if (m_wasConnected && m_pClientCallbacks != nullptr) { + m_pClientCallbacks->onDisconnect(this); + } break; } // ESP_GATTC_DISCONNECT_EVT @@ -228,7 +251,8 @@ void BLEClient::gattClientEventHandler( // case ESP_GATTC_REG_EVT: { m_gattc_if = gattc_if; - m_semaphoreRegEvt.give(); + // pass on the registration status result, in case of failure + m_semaphoreRegEvt.give(evtParam->reg.status); break; } // ESP_GATTC_REG_EVT