From b7825111bb21b7822be37b3e143e6d968876f619 Mon Sep 17 00:00:00 2001 From: Yurii Date: Wed, 22 Oct 2025 20:37:36 +0300 Subject: [PATCH] refactor: active + passive BLE scanning --- platformio.ini | 8 ++++---- src/SensorsTask.h | 30 ++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/platformio.ini b/platformio.ini index 21c758e..d28031c 100644 --- a/platformio.ini +++ b/platformio.ini @@ -75,8 +75,7 @@ board_build.partitions = esp32_partitions.csv lib_deps = ${env.lib_deps} laxilef/ESP32Scheduler@^1.0.1 nimble_lib = h2zero/NimBLE-Arduino@2.3.6 -lib_ignore = ble - BluetoothSerial +lib_ignore = BluetoothSerial SimpleBLE ESP RainMaker RainMaker @@ -164,7 +163,6 @@ extends = esp32_defaults board = lolin_s2_mini build_unflags = -DARDUINO_USB_MODE=1 build_flags = ${esp32_defaults.build_flags} - -D MYNEWT_VAL_BLE_EXT_ADV=1 -D ARDUINO_USB_MODE=0 -D ARDUINO_USB_CDC_ON_BOOT=1 -D DEFAULT_OT_IN_GPIO=33 @@ -178,12 +176,14 @@ build_flags = ${esp32_defaults.build_flags} extends = esp32_defaults board = lolin_s3_mini lib_deps = ${esp32_defaults.lib_deps} - ${esp32_defaults.nimble_lib} + ;${esp32_defaults.nimble_lib} + https://github.com/h2zero/NimBLE-Arduino build_unflags = -DARDUINO_USB_MODE=1 build_flags = ${esp32_defaults.build_flags} -D ARDUINO_USB_MODE=0 -D ARDUINO_USB_CDC_ON_BOOT=1 -D USE_BLE=1 + -D MYNEWT_VAL_BLE_EXT_ADV=1 -D DEFAULT_OT_IN_GPIO=35 -D DEFAULT_OT_OUT_GPIO=36 -D DEFAULT_SENSOR_OUTDOOR_GPIO=13 diff --git a/src/SensorsTask.h b/src/SensorsTask.h index 292ab48..e386f90 100644 --- a/src/SensorsTask.h +++ b/src/SensorsTask.h @@ -152,7 +152,7 @@ public: this->dallasLastPollingTime.reserve(2); #if USE_BLE - this->bluetoothScanCallbacks = new BluetoothScanCallbacks(); + this->pBLEScanCallbacks = new BluetoothScanCallbacks(); #endif } @@ -164,7 +164,7 @@ public: this->dallasLastPollingTime.clear(); #if USE_BLE - delete this->bluetoothScanCallbacks; + delete this->pBLEScanCallbacks; #endif } @@ -181,7 +181,8 @@ protected: std::unordered_map dallasLastPollingTime; #if USE_BLE NimBLEScan* pBLEScan = nullptr; - BluetoothScanCallbacks* bluetoothScanCallbacks = nullptr; + BluetoothScanCallbacks* pBLEScanCallbacks = nullptr; + bool activeScanBle = false; #endif unsigned long globalLastPollingTime = 0; @@ -569,7 +570,7 @@ protected: if (!NimBLEDevice::isInitialized() && millis() > 5000) { Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Initialized")); - BLEDevice::init(""); + NimBLEDevice::init(""); #if defined(ESP_PWR_LVL_P20) NimBLEDevice::setPower(ESP_PWR_LVL_P20); @@ -580,19 +581,28 @@ protected: if (this->pBLEScan == nullptr) { this->pBLEScan = NimBLEDevice::getScan(); - this->pBLEScan->setScanCallbacks(this->bluetoothScanCallbacks); - this->pBLEScan->setActiveScan(false); + this->pBLEScan->setScanCallbacks(this->pBLEScanCallbacks); + #if MYNEWT_VAL(BLE_EXT_ADV) + this->pBLEScan->setPhy(NimBLEScan::Phy::SCAN_ALL); + #endif this->pBLEScan->setDuplicateFilter(false); this->pBLEScan->setMaxResults(0); - this->pBLEScan->setInterval(10); - this->pBLEScan->setWindow(10); + this->pBLEScan->setInterval(10000); + this->pBLEScan->setWindow(10000); Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Scanning initialized")); } if (!this->pBLEScan->isScanning()) { - if (this->pBLEScan->start(0, false, true)) { - Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Scanning started")); + this->activeScanBle = !this->activeScanBle; + this->pBLEScan->setActiveScan(this->activeScanBle); + + if (this->pBLEScan->start(30000, false, false)) { + Log.sinfoln( + FPSTR(L_SENSORS_BLE), + F("%s scanning started"), + this->activeScanBle ? "Active" : "Passive" + ); } else { Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Unable to start scanning"));