1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-05 03:13:49 +01:00

make ble client disable/enable smarter

This commit is contained in:
J. Nick Koston
2025-06-17 18:09:53 +02:00
parent 1c05115bf5
commit 5453835963
2 changed files with 15 additions and 10 deletions

View File

@@ -22,6 +22,19 @@ void BLEClientBase::setup() {
this->connection_index_ = connection_index++;
}
void BLEClientBase::set_state(espbt::ClientState st) {
ESP_LOGV(TAG, "[%d] [%s] Set state %d", this->connection_index_, this->address_str_.c_str(), (int) st);
ESPBTClient::set_state(st);
// Disable loop when idle AND address is not set (unused connection slot)
if (st == espbt::ClientState::IDLE && this->address_ == 0) {
this->disable_loop();
} else if (st == espbt::ClientState::READY_TO_CONNECT || st == espbt::ClientState::INIT) {
// Enable loop when we need to initialize or connect
this->enable_loop();
}
}
void BLEClientBase::loop() {
if (!esp32_ble::global_ble->is_active()) {
this->set_state(espbt::ClientState::INIT);
@@ -36,12 +49,6 @@ void BLEClientBase::loop() {
this->set_state(espbt::ClientState::IDLE);
}
// If address is 0, this connection is not in use
if (this->address_ == 0) {
this->disable_loop();
return;
}
// READY_TO_CONNECT means we have discovered the device
// and the scanner has been stopped by the tracker.
if (this->state_ == espbt::ClientState::READY_TO_CONNECT) {

View File

@@ -63,10 +63,6 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
(uint8_t) (this->address_ >> 16) & 0xff, (uint8_t) (this->address_ >> 8) & 0xff,
(uint8_t) (this->address_ >> 0) & 0xff);
}
// Re-enable loop() when a non-zero address is assigned
if (address != 0) {
this->enable_loop();
}
}
std::string address_str() const { return this->address_str_; }
@@ -97,6 +93,8 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
bool check_addr(esp_bd_addr_t &addr) { return memcmp(addr, this->remote_bda_, sizeof(esp_bd_addr_t)) == 0; }
void set_state(espbt::ClientState st) override;
protected:
int gattc_if_;
esp_bd_addr_t remote_bda_;