From a4043e3f04f4e3c37f06c65f9f4e389d04f8211c Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 24 Nov 2024 19:46:34 -0700 Subject: [PATCH] Add NimBLEClientCallbacks::onConnectFail callback Adds a callback that is called when the connection attempt fail while connecting asynchronously. --- src/NimBLEClient.cpp | 32 ++++++++++++++++++-------------- src/NimBLEClient.h | 9 ++++++++- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 7054f93..1a49e0d 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -977,28 +977,28 @@ int NimBLEClient::handleGapEvent(struct ble_gap_event* event, void* arg) { rc = event->connect.status; if (rc == 0) { pClient->m_connHandle = event->connect.conn_handle; + + if (pClient->m_config.asyncConnect) { + pClient->m_pClientCallbacks->onConnect(pClient); + } + if (pClient->m_config.exchangeMTU) { - if (!pClient->exchangeMTU() && !pClient->m_config.asyncConnect) { + if (!pClient->exchangeMTU()) { rc = pClient->m_lastErr; // sets the error in the task data break; } } } else { pClient->m_connHandle = BLE_HS_CONN_HANDLE_NONE; - if (!pClient->m_config.asyncConnect) { - break; + + if (pClient->m_config.asyncConnect) { + pClient->m_pClientCallbacks->onConnectFail(pClient, rc); + if (pClient->m_config.deleteOnConnectFail) { + NimBLEDevice::deleteClient(pClient); + } } - if (pClient->m_config.deleteOnConnectFail) { // async connect - delete pClient; - return 0; - } - } - - if (pClient->m_config.asyncConnect) { - pClient->m_pClientCallbacks->onConnect(pClient); - } else if (!pClient->m_config.exchangeMTU) { - break; // not waiting for MTU exchange so release the task now. + break; } return 0; @@ -1236,8 +1236,12 @@ void NimBLEClientCallbacks::onConnect(NimBLEClient* pClient) { NIMBLE_LOGD(CB_TAG, "onConnect: default"); } +void NimBLEClientCallbacks::onConnectFail(NimBLEClient* pClient, int reason) { + NIMBLE_LOGD(CB_TAG, "onConnectFail: default, reason: %d", reason); +} + void NimBLEClientCallbacks::onDisconnect(NimBLEClient* pClient, int reason) { - NIMBLE_LOGD(CB_TAG, "onDisconnect: default"); + NIMBLE_LOGD(CB_TAG, "onDisconnect: default, reason: %d", reason); } bool NimBLEClientCallbacks::onConnParamsUpdateRequest(NimBLEClient* pClient, const ble_gap_upd_params* params) { diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index 430e3c6..8a23cf8 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -148,10 +148,17 @@ class NimBLEClientCallbacks { /** * @brief Called after client connects. - * @param [in] pClient A pointer to the calling client object. + * @param [in] pClient A pointer to the connecting client object. */ virtual void onConnect(NimBLEClient* pClient); + /** + * @brief Called when a connection attempt fails. + * @param [in] pClient A pointer to the connecting client object. + * @param [in] reason Contains the reason code for the connection failure. + */ + virtual void onConnectFail(NimBLEClient* pClient, int reason); + /** * @brief Called when disconnected from the server. * @param [in] pClient A pointer to the calling client object.