diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp index 1911701632..efd258a456 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.cpp +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.cpp @@ -35,11 +35,34 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga api::global_api_server->send_bluetooth_connections_free(this->proxy_->get_bluetooth_connections_free(), this->proxy_->get_bluetooth_connections_limit()); } + this->seen_mtu_or_services_ = false; + break; + } + case ESP_GATTC_CFG_MTU_EVT: { + if (param->cfg_mtu.conn_id != this->conn_id_) + break; + if (!this->seen_mtu_or_services_) { + // We don't know if we will get the MTU or the services first, so + // only send the device connection true if we have already received + // the services. + this->seen_mtu_or_services_ = true; + break; + } + api::global_api_server->send_bluetooth_device_connection(this->address_, true, this->mtu_); + api::global_api_server->send_bluetooth_connections_free(this->proxy_->get_bluetooth_connections_free(), + this->proxy_->get_bluetooth_connections_limit()); break; } case ESP_GATTC_SEARCH_CMPL_EVT: { if (param->search_cmpl.conn_id != this->conn_id_) break; + if (!this->seen_mtu_or_services_) { + // We don't know if we will get the MTU or the services first, so + // only send the device connection true if we have already received + // the mtu. + this->seen_mtu_or_services_ = true; + break; + } api::global_api_server->send_bluetooth_device_connection(this->address_, true, this->mtu_); api::global_api_server->send_bluetooth_connections_free(this->proxy_->get_bluetooth_connections_free(), this->proxy_->get_bluetooth_connections_limit()); diff --git a/esphome/components/bluetooth_proxy/bluetooth_connection.h b/esphome/components/bluetooth_proxy/bluetooth_connection.h index 1f759c3485..fa0233d2fb 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_connection.h +++ b/esphome/components/bluetooth_proxy/bluetooth_connection.h @@ -23,6 +23,7 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase { protected: friend class BluetoothProxy; + bool seen_mtu_or_services_{false}; int16_t send_service_{-1}; BluetoothProxy *proxy_; diff --git a/esphome/components/esp32_ble_client/ble_client_base.cpp b/esphome/components/esp32_ble_client/ble_client_base.cpp index 23a55727e4..9c7f98470b 100644 --- a/esphome/components/esp32_ble_client/ble_client_base.cpp +++ b/esphome/components/esp32_ble_client/ble_client_base.cpp @@ -112,6 +112,7 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_send_mtu_req failed, status=%x", this->connection_index_, this->address_str_.c_str(), ret); } + esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id, nullptr); break; } case ESP_GATTC_CFG_MTU_EVT: { @@ -124,7 +125,6 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_ ESP_LOGV(TAG, "[%d] [%s] cfg_mtu status %d, mtu %d", this->connection_index_, this->address_str_.c_str(), param->cfg_mtu.status, param->cfg_mtu.mtu); this->mtu_ = param->cfg_mtu.mtu; - esp_ble_gattc_search_service(esp_gattc_if, param->cfg_mtu.conn_id, nullptr); break; } case ESP_GATTC_DISCONNECT_EVT: {