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.
This commit is contained in:
h2zero authored and GitHub committed 2021-05-16 20:12:04 -05:00
1 parent e3731bdccf
commit c82f6cadb6
1 file changed
+4 -8
+4 -8
View File
@@ -649,8 +649,6 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
void BLEscan() { void BLEscan() {
disableCore0WDT(); disableCore0WDT();
Log.notice(F("Scan begin" CR)); Log.notice(F("Scan begin" CR));
BLEDevice::setScanDuplicateCacheSize(BLEScanDuplicateCacheSize);
BLEDevice::init("");
BLEScan* pBLEScan = BLEDevice::getScan(); BLEScan* pBLEScan = BLEDevice::getScan();
MyAdvertisedDeviceCallbacks myCallbacks; MyAdvertisedDeviceCallbacks myCallbacks;
pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks); pBLEScan->setAdvertisedDeviceCallbacks(&myCallbacks);
@@ -660,7 +658,6 @@ void BLEscan() {
BLEScanResults foundDevices = pBLEScan->start(Scan_duration / 1000, false); BLEScanResults foundDevices = pBLEScan->start(Scan_duration / 1000, false);
scanCount++; scanCount++;
Log.notice(F("Found %d devices, scan number %d end deinit controller" CR), foundDevices.getCount(), scanCount); Log.notice(F("Found %d devices, scan number %d end deinit controller" CR), foundDevices.getCount(), scanCount);
BLEDevice::deinit(true);
enableCore0WDT(); enableCore0WDT();
} }
@@ -713,7 +710,6 @@ void notifyCB(
*/ */
void BLEconnect() { void BLEconnect() {
Log.notice(F("BLE Connect begin" CR)); Log.notice(F("BLE Connect begin" CR));
BLEDevice::init("");
for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) { for (vector<BLEdevice*>::iterator it = devices.begin(); it != devices.end(); ++it) {
BLEdevice* p = *it; BLEdevice* p = *it;
if (p->sensorModel == LYWSD03MMC || p->sensorModel == MHO_C401) { if (p->sensorModel == LYWSD03MMC || p->sensorModel == MHO_C401) {
@@ -819,10 +815,7 @@ void deepSleep(uint64_t time_in_us) {
# endif # endif
Log.trace(F("Deactivating ESP32 components" CR)); Log.trace(F("Deactivating ESP32 components" CR));
esp_bluedroid_disable(); BLEDevice::deinit(true);
esp_bluedroid_deinit();
esp_bt_controller_disable();
esp_bt_controller_deinit();
esp_bt_mem_release(ESP_BT_MODE_BTDM); esp_bt_mem_release(ESP_BT_MODE_BTDM);
// Ignore the deprecated warning, this call is necessary here. // Ignore the deprecated warning, this call is necessary here.
# pragma GCC diagnostic push # pragma GCC diagnostic push
@@ -866,6 +859,9 @@ void setupBT() {
atomic_init(&jsonBTBufferQueueNext, 0); // in theory, we don't need this atomic_init(&jsonBTBufferQueueNext, 0); // in theory, we don't need this
atomic_init(&jsonBTBufferQueueLast, 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 // we setup a task with priority one to avoid conflict with other gateways
xTaskCreatePinnedToCore( xTaskCreatePinnedToCore(
coreTask, /* Function to implement the task */ coreTask, /* Function to implement the task */