Skip to content

Commit ee7c53f

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 20097ff commit ee7c53f

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,8 +933,15 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) {
933933

934934
switch (event->type) {
935935
case BLE_GAP_EVENT_DISCONNECT: {
936+
NimBLEConnInfo peerInfo;
937+
rc = ble_gap_conn_find(event->disconnect.conn.conn_handle, &peerInfo.m_desc);
938+
if (rc != 0) {
939+
NIMBLE_LOGE(LOG_TAG, "Failed to find connection info; rc=%d", rc);
940+
return 0;
941+
}
942+
936943
// workaround for bug in NimBLE stack where disconnect event argument is not passed correctly
937-
pClient = NimBLEDevice::getClientByHandle(event->disconnect.conn.conn_handle);
944+
pClient = NimBLEDevice::getClientByPeerAddress(peerInfo.m_desc.peer_id_addr);
938945
if (pClient == nullptr) {
939946
return 0;
940947
}
@@ -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)