Fix endless loop when calling scan start from scan end callback.

When attempting to connect and the scanner is running `NimBLEScan::stop` is called to stop it
which then calls the `onScanEnded` callback. If the app then starts the scan again in this
callback an endless loop will be created.

This change prevents the endless loop by setting a flag in `NimBLEDevice` that is checked before
starting a scan while a client is trying to connect.

* Adds `NimBLEDevice::setConnectionInProgress` and `NimBLEDevice::isConnectionInProgress` functions.
This commit is contained in:
h2zero
2024-11-10 10:36:51 -07:00
committed by h2zero
parent 7d0636bc91
commit 724e1a7083
4 changed files with 37 additions and 2 deletions
+21
View File
@@ -101,6 +101,10 @@ std::vector<NimBLEAddress> NimBLEDevice::m_ignoreList{};
std::vector<NimBLEAddress> NimBLEDevice::m_whiteList{};
uint8_t NimBLEDevice::m_ownAddrType{BLE_OWN_ADDR_PUBLIC};
# if defined(CONFIG_BT_NIMBLE_ROLE_CENTRAL) || defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
bool NimBLEDevice::m_connectionInProgress{false};
# endif
# ifdef ESP_PLATFORM
# ifdef CONFIG_BTDM_BLE_SCAN_DUPL
uint16_t NimBLEDevice::m_scanDuplicateSize{CONFIG_BTDM_SCAN_DUPL_CACHE_SIZE};
@@ -1232,6 +1236,23 @@ bool NimBLEDevice::setCustomGapHandler(gap_event_handler handler) {
return rc == 0;
} // setCustomGapHandler
/**
* @brief Set the connection in progress flag.
* @param [in] inProgress The connection in progress flag.
* @details This is used to prevent a scan from starting while a connection is in progress.
*/
void NimBLEDevice::setConnectionInProgress(bool inProgress) {
m_connectionInProgress = inProgress;
} // setConnectionInProgress
/**
* @brief Check if a connection is in progress.
* @return True if a connection is in progress.
*/
bool NimBLEDevice::isConnectionInProgress() {
return m_connectionInProgress;
} // isConnectionInProgress
/**
* @brief Return a string representation of the address of this device.
* @return A string representation of this device address.