From 856adebad5aa164bcce1ff3bfe27599440177eb1 Mon Sep 17 00:00:00 2001 From: h2zero Date: Mon, 14 Apr 2025 08:39:28 -0600 Subject: [PATCH] Fix potential exception when scan is restarted. If the scan is restarted in NimBLEScan::start, there is a chance that a result from the previous scan arrives and the device is deleted when the callback is called. This change ignores any results that come when the scan has stopped. --- src/NimBLEScan.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/NimBLEScan.cpp b/src/NimBLEScan.cpp index af0dd71..bae4b4f 100644 --- a/src/NimBLEScan.cpp +++ b/src/NimBLEScan.cpp @@ -54,6 +54,11 @@ int NimBLEScan::handleGapEvent(ble_gap_event* event, void* arg) { (void)arg; NimBLEScan* pScan = NimBLEDevice::getScan(); + if (!pScan->isScanning()) { + NIMBLE_LOGI(LOG_TAG, "Scan stopped, ignoring event"); + return 0; + } + switch (event->type) { case BLE_GAP_EVENT_EXT_DISC: case BLE_GAP_EVENT_DISC: {