2 Commits

Author SHA1 Message Date
Yurii
b7825111bb refactor: active + passive BLE scanning 2025-10-22 20:37:36 +03:00
Yurii
d5691ef8f7 refactor: decreased interval and window for scanning BLE 2025-10-22 17:22:02 +03:00
2 changed files with 24 additions and 13 deletions

View File

@@ -75,8 +75,7 @@ board_build.partitions = esp32_partitions.csv
lib_deps = ${env.lib_deps} lib_deps = ${env.lib_deps}
laxilef/ESP32Scheduler@^1.0.1 laxilef/ESP32Scheduler@^1.0.1
nimble_lib = h2zero/NimBLE-Arduino@2.3.6 nimble_lib = h2zero/NimBLE-Arduino@2.3.6
lib_ignore = ble lib_ignore = BluetoothSerial
BluetoothSerial
SimpleBLE SimpleBLE
ESP RainMaker ESP RainMaker
RainMaker RainMaker
@@ -177,12 +176,14 @@ build_flags = ${esp32_defaults.build_flags}
extends = esp32_defaults extends = esp32_defaults
board = lolin_s3_mini board = lolin_s3_mini
lib_deps = ${esp32_defaults.lib_deps} 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_unflags = -DARDUINO_USB_MODE=1
build_flags = ${esp32_defaults.build_flags} build_flags = ${esp32_defaults.build_flags}
-D ARDUINO_USB_MODE=0 -D ARDUINO_USB_MODE=0
-D ARDUINO_USB_CDC_ON_BOOT=1 -D ARDUINO_USB_CDC_ON_BOOT=1
-D USE_BLE=1 -D USE_BLE=1
-D MYNEWT_VAL_BLE_EXT_ADV=1
-D DEFAULT_OT_IN_GPIO=35 -D DEFAULT_OT_IN_GPIO=35
-D DEFAULT_OT_OUT_GPIO=36 -D DEFAULT_OT_OUT_GPIO=36
-D DEFAULT_SENSOR_OUTDOOR_GPIO=13 -D DEFAULT_SENSOR_OUTDOOR_GPIO=13

View File

@@ -152,7 +152,7 @@ public:
this->dallasLastPollingTime.reserve(2); this->dallasLastPollingTime.reserve(2);
#if USE_BLE #if USE_BLE
this->bluetoothScanCallbacks = new BluetoothScanCallbacks(); this->pBLEScanCallbacks = new BluetoothScanCallbacks();
#endif #endif
} }
@@ -164,7 +164,7 @@ public:
this->dallasLastPollingTime.clear(); this->dallasLastPollingTime.clear();
#if USE_BLE #if USE_BLE
delete this->bluetoothScanCallbacks; delete this->pBLEScanCallbacks;
#endif #endif
} }
@@ -181,7 +181,8 @@ protected:
std::unordered_map<uint8_t, unsigned long> dallasLastPollingTime; std::unordered_map<uint8_t, unsigned long> dallasLastPollingTime;
#if USE_BLE #if USE_BLE
NimBLEScan* pBLEScan = nullptr; NimBLEScan* pBLEScan = nullptr;
BluetoothScanCallbacks* bluetoothScanCallbacks = nullptr; BluetoothScanCallbacks* pBLEScanCallbacks = nullptr;
bool activeScanBle = false;
#endif #endif
unsigned long globalLastPollingTime = 0; unsigned long globalLastPollingTime = 0;
@@ -569,7 +570,7 @@ protected:
if (!NimBLEDevice::isInitialized() && millis() > 5000) { if (!NimBLEDevice::isInitialized() && millis() > 5000) {
Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Initialized")); Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Initialized"));
BLEDevice::init(""); NimBLEDevice::init("");
#if defined(ESP_PWR_LVL_P20) #if defined(ESP_PWR_LVL_P20)
NimBLEDevice::setPower(ESP_PWR_LVL_P20); NimBLEDevice::setPower(ESP_PWR_LVL_P20);
@@ -580,19 +581,28 @@ protected:
if (this->pBLEScan == nullptr) { if (this->pBLEScan == nullptr) {
this->pBLEScan = NimBLEDevice::getScan(); this->pBLEScan = NimBLEDevice::getScan();
this->pBLEScan->setScanCallbacks(this->bluetoothScanCallbacks); this->pBLEScan->setScanCallbacks(this->pBLEScanCallbacks);
this->pBLEScan->setActiveScan(false); #if MYNEWT_VAL(BLE_EXT_ADV)
this->pBLEScan->setPhy(NimBLEScan::Phy::SCAN_ALL);
#endif
this->pBLEScan->setDuplicateFilter(false); this->pBLEScan->setDuplicateFilter(false);
this->pBLEScan->setMaxResults(0); this->pBLEScan->setMaxResults(0);
this->pBLEScan->setInterval(100); this->pBLEScan->setInterval(10000);
this->pBLEScan->setWindow(100); this->pBLEScan->setWindow(10000);
Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Scanning initialized")); Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Scanning initialized"));
} }
if (!this->pBLEScan->isScanning()) { if (!this->pBLEScan->isScanning()) {
if (this->pBLEScan->start(0, false, true)) { this->activeScanBle = !this->activeScanBle;
Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Scanning started")); 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 { } else {
Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Unable to start scanning")); Log.sinfoln(FPSTR(L_SENSORS_BLE), F("Unable to start scanning"));