1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-22 19:53:46 +01:00

Improve reliability of bluetooth active connections (#4049)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
J. Nick Koston
2022-11-28 09:15:40 -10:00
committed by GitHub
parent 75573a3ed1
commit 73c82862cf
4 changed files with 130 additions and 71 deletions

View File

@@ -128,6 +128,11 @@ BluetoothConnection *BluetoothProxy::get_connection_(uint64_t address, bool rese
for (auto *connection : this->connections_) {
if (connection->get_address() == 0) {
connection->set_address(address);
// All connections must start at INIT
// We only set the state if we allocate the connection
// to avoid a race where multiple connection attempts
// are made.
connection->set_state(espbt::ClientState::INIT);
return connection;
}
}
@@ -144,8 +149,19 @@ void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest
api::global_api_server->send_bluetooth_device_connection(msg.address, false);
return;
}
ESP_LOGV(TAG, "[%d] [%s] Searching to connect", connection->get_connection_index(),
connection->address_str().c_str());
if (connection->state() == espbt::ClientState::CONNECTED ||
connection->state() == espbt::ClientState::ESTABLISHED) {
ESP_LOGW(TAG, "[%d] [%s] Connection already established", connection->get_connection_index(),
connection->address_str().c_str());
api::global_api_server->send_bluetooth_device_connection(msg.address, true);
api::global_api_server->send_bluetooth_connections_free(this->get_bluetooth_connections_free(),
this->get_bluetooth_connections_limit());
return;
} else if (connection->state() != espbt::ClientState::INIT) {
ESP_LOGW(TAG, "[%d] [%s] Connection already in progress", connection->get_connection_index(),
connection->address_str().c_str());
return;
}
connection->set_state(espbt::ClientState::SEARCHING);
api::global_api_server->send_bluetooth_connections_free(this->get_bluetooth_connections_free(),
this->get_bluetooth_connections_limit());