From 2841ef957f6f6520d128b6d50ce62bb586e9c0a6 Mon Sep 17 00:00:00 2001
From: Ben79543 <49697001+Ben79543@users.noreply.github.com>
Date: Thu, 3 Jun 2021 16:12:05 +0200
Subject: [PATCH] Update BLEScan.cpp

Proposed fix to #4627 as a remediation to PR #3995
#3995 introduced that a device detected on BLE would not be entered in vector if a callback has been defined.
By doing so, it was not possible anymore to have a counter in a call back (AND device in vector) to limit the number of detected device in a single scan, which could crash ESP32 as a result.
---
 libraries/BLE/src/BLEScan.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libraries/BLE/src/BLEScan.cpp b/libraries/BLE/src/BLEScan.cpp
index c93a30c5fa3..e17eb2b0f12 100644
--- a/libraries/BLE/src/BLEScan.cpp
+++ b/libraries/BLE/src/BLEScan.cpp
@@ -126,7 +126,8 @@ void BLEScan::handleGAPEvent(
 
 					if (m_pAdvertisedDeviceCallbacks) { // if has callback, no need to record to vector
 						m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);
-					} else if (!m_wantDuplicates && !found) {   // if no callback and not want duplicate, and not already in vector, record it
+					} 
+					if (!m_wantDuplicates && !found) {   // if no callback and not want duplicate, and not already in vector, record it
 						m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
 						shouldDelete = false;
 					}