From c82f6cadb6f278cea60697344866ecd47be940bc Mon Sep 17 00:00:00 2001 From: h2zero <32826625+h2zero@users.noreply.github.com> Date: Sun, 16 May 2021 19:12:04 -0600 Subject: [PATCH] Remove reinitializing the BLE stack on each scan. (#949) Using NimBLE requires less resources than bluedroid so it is much less beneficial to deinitialize the stack. Instead this initializes it once in initial setup and only deinitializes before entering deep sleep. --- main/ZgatewayBT.ino | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index aeec574..9e22cfa 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -649,8 +649,6 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks { void BLEscan() { disableCore0WDT(); Log.notice(F("Scan begin" CR)); - BLEDevice::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize); - BLEDevice::init(""); BLEScan* pBLEScan = BLEDevice::getScan(); MyAdvertisedDeviceCallbacks myCallbacks; pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks); @@ -660,7 +658,6 @@ void BLEscan() { BLEScanResults foundDevices = pBLEScan->start(Scan_duration / 1000, false); scanCount++; Log.notice(F("Found %d devices, scan number %d end deinit controller" CR), foundDevices.getCount(), scanCount); - BLEDevice::deinit(true); enableCore0WDT(); } @@ -713,7 +710,6 @@ void notifyCB( */ void BLEconnect() { Log.notice(F("BLE Connect begin" CR)); - BLEDevice::init(""); for (vector::iterator it = devices.begin(); it != devices.end(); ++it) { BLEdevice* p = *it; if (p->sensorModel == LYWSD03MMC || p->sensorModel == MHO_C401) { @@ -819,10 +815,7 @@ void deepSleep(uint64_t time_in_us) { # endif Log.trace(F("Deactivating ESP32 components" CR)); - esp_bluedroid_disable(); - esp_bluedroid_deinit(); - esp_bt_controller_disable(); - esp_bt_controller_deinit(); + BLEDevice::deinit(true); esp_bt_mem_release(ESP_BT_MODE_BTDM); // Ignore the deprecated warning, this call is necessary here. # pragma GCC diagnostic push @@ -866,6 +859,9 @@ void setupBT() { atomic_init(&jsonBTBufferQueueNext, 0); // in theory, we don't need this atomic_init(&jsonBTBufferQueueLast, 0); // in theory, we don't need this + BLEDevice::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize); + BLEDevice::init(""); + // we setup a task with priority one to avoid conflict with other gateways xTaskCreatePinnedToCore( coreTask, /* Function to implement the task */