[esp32_ble, esp32_ble_tracker] Fix crash, error messages when ble.disable called during boot (#12560)

This commit is contained in:
Keith Burzinski
2025-12-18 19:42:47 -06:00
committed by Jonathan Swoboda
parent 7ca11764ab
commit f0d0ea60a7
3 changed files with 25 additions and 8 deletions

View File

@@ -308,13 +308,21 @@ bool ESP32BLE::ble_setup_() {
bool ESP32BLE::ble_dismantle_() { bool ESP32BLE::ble_dismantle_() {
esp_err_t err = esp_bluedroid_disable(); esp_err_t err = esp_bluedroid_disable();
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_bluedroid_disable failed: %d", err); // ESP_ERR_INVALID_STATE means Bluedroid is already disabled, which is fine
return false; if (err != ESP_ERR_INVALID_STATE) {
ESP_LOGE(TAG, "esp_bluedroid_disable failed: %d", err);
return false;
}
ESP_LOGD(TAG, "Already disabled");
} }
err = esp_bluedroid_deinit(); err = esp_bluedroid_deinit();
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_bluedroid_deinit failed: %d", err); // ESP_ERR_INVALID_STATE means Bluedroid is already deinitialized, which is fine
return false; if (err != ESP_ERR_INVALID_STATE) {
ESP_LOGE(TAG, "esp_bluedroid_deinit failed: %d", err);
return false;
}
ESP_LOGD(TAG, "Already deinitialized");
} }
#ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID #ifndef CONFIG_ESP_HOSTED_ENABLE_BT_BLUEDROID

View File

@@ -212,17 +212,23 @@ extern ESP32BLE *global_ble;
template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> { template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
public: public:
bool check(const Ts &...x) override { return global_ble->is_active(); } bool check(const Ts &...x) override { return global_ble != nullptr && global_ble->is_active(); }
}; };
template<typename... Ts> class BLEEnableAction : public Action<Ts...> { template<typename... Ts> class BLEEnableAction : public Action<Ts...> {
public: public:
void play(const Ts &...x) override { global_ble->enable(); } void play(const Ts &...x) override {
if (global_ble != nullptr)
global_ble->enable();
}
}; };
template<typename... Ts> class BLEDisableAction : public Action<Ts...> { template<typename... Ts> class BLEDisableAction : public Action<Ts...> {
public: public:
void play(const Ts &...x) override { global_ble->disable(); } void play(const Ts &...x) override {
if (global_ble != nullptr)
global_ble->disable();
}
}; };
} // namespace esphome::esp32_ble } // namespace esphome::esp32_ble

View File

@@ -185,7 +185,10 @@ void ESP32BLETracker::ble_before_disabled_event_handler() { this->stop_scan_();
void ESP32BLETracker::stop_scan_() { void ESP32BLETracker::stop_scan_() {
if (this->scanner_state_ != ScannerState::RUNNING && this->scanner_state_ != ScannerState::FAILED) { if (this->scanner_state_ != ScannerState::RUNNING && this->scanner_state_ != ScannerState::FAILED) {
ESP_LOGE(TAG, "Cannot stop scan: %s", this->scanner_state_to_string_(this->scanner_state_)); // If scanner is already idle, there's nothing to stop - this is not an error
if (this->scanner_state_ != ScannerState::IDLE) {
ESP_LOGE(TAG, "Cannot stop scan: %s", this->scanner_state_to_string_(this->scanner_state_));
}
return; return;
} }
// Reset timeout state machine when stopping scan // Reset timeout state machine when stopping scan