From ad0d6f6337afc9d8592c8b26456c3c27090e3ead Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 28 Nov 2022 18:11:54 -1000 Subject: [PATCH] Combine the scanner failure check so we only stop the scanner once (#4111) --- .../esp32_ble_tracker/esp32_ble_tracker.cpp | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index cdefd31288..50e59537f9 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -144,28 +144,31 @@ void ESP32BLETracker::loop() { xSemaphoreGive(this->scan_result_lock_); } - if (this->scan_set_param_failed_) { - ESP_LOGE(TAG, "Scan set param failed: %d", this->scan_set_param_failed_); - esp_ble_gap_stop_scanning(); - this->scan_set_param_failed_ = ESP_BT_STATUS_SUCCESS; - } - - if (this->scan_start_failed_) { - ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_); - esp_ble_gap_stop_scanning(); - this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS; - } - if (!connecting && xSemaphoreTake(this->scan_end_lock_, 0L)) { if (this->scan_continuous_) { - if (!promote_to_connecting) { + if (!promote_to_connecting && !this->scan_start_failed_ && !this->scan_set_param_failed_) { this->start_scan_(false); + } else { + // We didn't start the scan, so we need to release the lock + xSemaphoreGive(this->scan_end_lock_); } } else if (!this->scanner_idle_) { this->end_of_scan_(); return; } } + + if (this->scan_start_failed_ || this->scan_set_param_failed_) { + esp_ble_gap_stop_scanning(); + if (this->scan_start_failed_) { + ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_); + this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS; + } + if (this->scan_set_param_failed_) { + ESP_LOGE(TAG, "Scan set param failed: %d", this->scan_set_param_failed_); + this->scan_set_param_failed_ = ESP_BT_STATUS_SUCCESS; + } + } } // If there is a discovered client and no connecting