Skip to content

Commit 8cefc0a

Browse files
committed
[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.
1 parent 8af38e7 commit 8cefc0a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/NimBLEClient.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,9 +933,16 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
933933

934934
switch (event->type) {
935935
case BLE_GAP_EVENT_DISCONNECT: {
936+
936937
// workaround for bug in NimBLE stack where disconnect event argument is not passed correctly
937-
pClient = NimBLEDevice::getClientByHandle(event->disconnect.conn.conn_handle);
938+
pClient = NimBLEDevice::getClientByPeerAddress(event->disconnect.conn.peer_ota_addr);
938939
if (pClient == nullptr) {
940+
pClient = NimBLEDevice::getClientByPeerAddress(event->disconnect.conn.peer_id_addr);
941+
}
942+
943+
if (pClient == nullptr) {
944+
NIMBLE_LOGE(LOG_TAG, "Disconnected client not found, conn_handle=%d",
945+
event->disconnect.conn.conn_handle);
939946
return 0;
940947
}
941948

@@ -960,7 +967,9 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
960967
pClient->m_asyncSecureAttempt = 0;
961968

962969
// Don't call the disconnect callback if we are waiting for a connection to complete and it fails
963-
if (rc != (BLE_HS_ERR_HCI_BASE + BLE_ERR_CONN_ESTABLISHMENT) || pClient->m_config.asyncConnect) {
970+
if (rc == (BLE_HS_ERR_HCI_BASE + BLE_ERR_CONN_ESTABLISHMENT) && pClient->m_config.asyncConnect) {
971+
pClient->m_pClientCallbacks->onConnectFail(pClient, rc);
972+
} else {
964973
pClient->m_pClientCallbacks->onDisconnect(pClient, rc);
965974
}
966975

0 commit comments

Comments
 (0)