Skip to content

Commit 95ed28c

Browse files
authored
Remove reinitializing the BLE stack on each scan. (1technophile#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.
1 parent 8d5584f commit 95ed28c

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

main/ZgatewayBT.ino

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -649,8 +649,6 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
649649
void BLEscan() {
650650
disableCore0WDT();
651651
Log.notice(F("Scan begin" CR));
652-
BLEDevice::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize);
653-
BLEDevice::init("");
654652
BLEScan* pBLEScan = BLEDevice::getScan();
655653
MyAdvertisedDeviceCallbacks myCallbacks;
656654
pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks);
@@ -660,7 +658,6 @@ void BLEscan() {
660658
BLEScanResults foundDevices = pBLEScan->start(Scan_duration / 1000, false);
661659
scanCount++;
662660
Log.notice(F("Found %d devices, scan number %d end deinit controller" CR), foundDevices.getCount(), scanCount);
663-
BLEDevice::deinit(true);
664661
enableCore0WDT();
665662
}
666663

@@ -713,7 +710,6 @@ void notifyCB(
713710
*/
714711
void BLEconnect() {
715712
Log.notice(F("BLE Connect begin" CR));
716-
BLEDevice::init("");
717713
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
718714
BLEdevice* p = *it;
719715
if (p->sensorModel == LYWSD03MMC || p->sensorModel == MHO_C401) {
@@ -819,10 +815,7 @@ void deepSleep(uint64_t time_in_us) {
819815
# endif
820816

821817
Log.trace(F("Deactivating ESP32 components" CR));
822-
esp_bluedroid_disable();
823-
esp_bluedroid_deinit();
824-
esp_bt_controller_disable();
825-
esp_bt_controller_deinit();
818+
BLEDevice::deinit(true);
826819
esp_bt_mem_release(ESP_BT_MODE_BTDM);
827820
// Ignore the deprecated warning, this call is necessary here.
828821
# pragma GCC diagnostic push
@@ -866,6 +859,9 @@ void setupBT() {
866859
atomic_init(&jsonBTBufferQueueNext, 0); // in theory, we don't need this
867860
atomic_init(&jsonBTBufferQueueLast, 0); // in theory, we don't need this
868861

862+
BLEDevice::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize);
863+
BLEDevice::init("");
864+
869865
// we setup a task with priority one to avoid conflict with other gateways
870866
xTaskCreatePinnedToCore(
871867
coreTask, /* Function to implement the task */

0 commit comments

Comments
 (0)