[Bugfix] OnConnectfail not called when connection not established.

Workaround for when the disconnect event is sent when no connection has been established.
Espressif changed this from a connect event with error code to disconnect event.
This commit is contained in:
h2zero
2025-07-31 17:36:40 -06:00
parent 8af38e7eb9
commit 8cefc0a562

View File

@@ -933,9 +933,16 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
switch (event->type) { switch (event->type) {
case BLE_GAP_EVENT_DISCONNECT: { case BLE_GAP_EVENT_DISCONNECT: {
// workaround for bug in NimBLE stack where disconnect event argument is not passed correctly // workaround for bug in NimBLE stack where disconnect event argument is not passed correctly
pClient = NimBLEDevice::getClientByHandle(event->disconnect.conn.conn_handle); pClient = NimBLEDevice::getClientByPeerAddress(event->disconnect.conn.peer_ota_addr);
if (pClient == nullptr) { if (pClient == nullptr) {
pClient = NimBLEDevice::getClientByPeerAddress(event->disconnect.conn.peer_id_addr);
}
if (pClient == nullptr) {
NIMBLE_LOGE(LOG_TAG, "Disconnected client not found, conn_handle=%d",
event->disconnect.conn.conn_handle);
return 0; return 0;
} }
@@ -960,7 +967,9 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
pClient->m_asyncSecureAttempt = 0; pClient->m_asyncSecureAttempt = 0;
// Don't call the disconnect callback if we are waiting for a connection to complete and it fails // Don't call the disconnect callback if we are waiting for a connection to complete and it fails
if (rc != (BLE_HS_ERR_HCI_BASE + BLE_ERR_CONN_ESTABLISHMENT) || pClient->m_config.asyncConnect) { if (rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_CONN_ESTABLISHMENT) && pClient->m_config.asyncConnect) {
pClient->m_pClientCallbacks->onConnectFail(pClient, rc);
} else {
pClient->m_pClientCallbacks->onDisconnect(pClient, rc); pClient->m_pClientCallbacks->onDisconnect(pClient, rc);
} }