From 65e05e6c57658b9f6af3ce10e947a83970dbe54c Mon Sep 17 00:00:00 2001 From: h2zero Date: Sun, 10 Nov 2024 09:11:52 -0700 Subject: [PATCH] Add NimBLEClient::cancelConnect Adds a function to cancel and in-progress connection. --- src/NimBLEClient.cpp | 15 +++++++++++++++ src/NimBLEClient.h | 1 + 2 files changed, 16 insertions(+) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 86a6bb6..8c141c0 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -369,6 +369,21 @@ bool NimBLEClient::disconnect(uint8_t reason) { return true; } // disconnect +/** + * @brief Cancel an ongoing connection attempt. + * @return True if the command was successfully sent. + */ +bool NimBLEClient::cancelConnect() { + int rc = ble_gap_conn_cancel(); + if (rc != 0 && rc != BLE_HS_EALREADY) { + NIMBLE_LOGE(LOG_TAG, "ble_gap_conn_cancel failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc)); + m_lastErr = rc; + return false; + } + + return true; +} // cancelConnect + # if CONFIG_BT_NIMBLE_EXT_ADV /** * @brief Set the PHY types to use when connecting to a server. diff --git a/src/NimBLEClient.h b/src/NimBLEClient.h index c9ec090..7b0b8e9 100644 --- a/src/NimBLEClient.h +++ b/src/NimBLEClient.h @@ -51,6 +51,7 @@ class NimBLEClient { bool connect(const NimBLEAddress& address, bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true); bool connect(bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true); bool disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM); + bool cancelConnect(); NimBLEAddress getPeerAddress() const; bool setPeerAddress(const NimBLEAddress& address); int getRssi() const;