mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2026-01-28 01:52:27 +01:00
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:
@@ -182,6 +182,9 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes)
|
||||
m_pTaskData = &taskData;
|
||||
int rc = 0;
|
||||
|
||||
// Set the connection in progress flag to prevent a scan from starting while connecting.
|
||||
NimBLEDevice::setConnectionInProgress(true);
|
||||
|
||||
do {
|
||||
# if CONFIG_BT_NIMBLE_EXT_ADV
|
||||
rc = ble_gap_ext_connect(NimBLEDevice::m_ownAddrType,
|
||||
@@ -207,7 +210,7 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes)
|
||||
break;
|
||||
|
||||
case BLE_HS_EBUSY:
|
||||
// Scan was still running, stop it and try again
|
||||
// Scan was active, stop it through the NimBLEScan API to release any tasks and call the callback.
|
||||
if (!NimBLEDevice::getScan()->stop()) {
|
||||
rc = BLE_HS_EUNKNOWN;
|
||||
}
|
||||
@@ -236,8 +239,8 @@ bool NimBLEClient::connect(const NimBLEAddress& address, bool deleteAttributes)
|
||||
|
||||
} while (rc == BLE_HS_EBUSY);
|
||||
|
||||
NimBLEDevice::setConnectionInProgress(false);
|
||||
m_lastErr = rc;
|
||||
|
||||
if (rc != 0) {
|
||||
m_pTaskData = nullptr;
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user