Skip to content

Commit 65e05e6

Browse files
committed
Add NimBLEClient::cancelConnect
Adds a function to cancel and in-progress connection.
1 parent 38e764d commit 65e05e6

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/NimBLEClient.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,21 @@ bool NimBLEClient::disconnect(uint8_t reason) {
369369
return true;
370370
} // disconnect
371371

372+
/**
373+
* @brief Cancel an ongoing connection attempt.
374+
* @return True if the command was successfully sent.
375+
*/
376+
bool NimBLEClient::cancelConnect() {
377+
int rc = ble_gap_conn_cancel();
378+
if (rc != 0 && rc != BLE_HS_EALREADY) {
379+
NIMBLE_LOGE(LOG_TAG, "ble_gap_conn_cancel failed: rc=%d %s", rc, NimBLEUtils::returnCodeToString(rc));
380+
m_lastErr = rc;
381+
return false;
382+
}
383+
384+
return true;
385+
} // cancelConnect
386+
372387
# if CONFIG_BT_NIMBLE_EXT_ADV
373388
/**
374389
* @brief Set the PHY types to use when connecting to a server.

src/NimBLEClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class NimBLEClient {
5151
bool connect(const NimBLEAddress& address, bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true);
5252
bool connect(bool deleteAttributes = true, bool asyncConnect = false, bool exchangeMTU = true);
5353
bool disconnect(uint8_t reason = BLE_ERR_REM_USER_CONN_TERM);
54+
bool cancelConnect();
5455
NimBLEAddress getPeerAddress() const;
5556
bool setPeerAddress(const NimBLEAddress& address);
5657
int getRssi() const;

0 commit comments

Comments
 (0)