mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 06:33:51 +00:00 
			
		
		
		
	feat: support ble_client that use security w/o pin codes (#3320)
This commit is contained in:
		| @@ -118,16 +118,21 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es | ||||
|         this->set_states_(espbt::ClientState::IDLE); | ||||
|         break; | ||||
|       } | ||||
|       this->conn_id = param->open.conn_id; | ||||
|       auto ret = esp_ble_gattc_send_mtu_req(this->gattc_if, param->open.conn_id); | ||||
|       break; | ||||
|     } | ||||
|     case ESP_GATTC_CONNECT_EVT: { | ||||
|       ESP_LOGV(TAG, "[%s] ESP_GATTC_CONNECT_EVT", this->address_str().c_str()); | ||||
|       this->conn_id = param->connect.conn_id; | ||||
|       auto ret = esp_ble_gattc_send_mtu_req(this->gattc_if, param->connect.conn_id); | ||||
|       if (ret) { | ||||
|         ESP_LOGW(TAG, "esp_ble_gattc_send_mtu_req failed, status=%d", ret); | ||||
|         ESP_LOGW(TAG, "esp_ble_gattc_send_mtu_req failed, status=%x", ret); | ||||
|       } | ||||
|       break; | ||||
|     } | ||||
|     case ESP_GATTC_CFG_MTU_EVT: { | ||||
|       if (param->cfg_mtu.status != ESP_GATT_OK) { | ||||
|         ESP_LOGW(TAG, "cfg_mtu to %s failed, status %d", this->address_str().c_str(), param->cfg_mtu.status); | ||||
|         ESP_LOGW(TAG, "cfg_mtu to %s failed, mtu %d, status %d", this->address_str().c_str(), param->cfg_mtu.mtu, | ||||
|                  param->cfg_mtu.status); | ||||
|         this->set_states_(espbt::ClientState::IDLE); | ||||
|         break; | ||||
|       } | ||||
| @@ -139,7 +144,7 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es | ||||
|       if (memcmp(param->disconnect.remote_bda, this->remote_bda, 6) != 0) { | ||||
|         return; | ||||
|       } | ||||
|       ESP_LOGV(TAG, "[%s] ESP_GATTC_DISCONNECT_EVT", this->address_str().c_str()); | ||||
|       ESP_LOGV(TAG, "[%s] ESP_GATTC_DISCONNECT_EVT, reason %d", this->address_str().c_str(), param->disconnect.reason); | ||||
|       for (auto &svc : this->services_) | ||||
|         delete svc;  // NOLINT(cppcoreguidelines-owning-memory) | ||||
|       this->services_.clear(); | ||||
| @@ -201,6 +206,32 @@ void BLEClient::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t es | ||||
|   } | ||||
| } | ||||
|  | ||||
| void BLEClient::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { | ||||
|   switch (event) { | ||||
|     // This event is sent by the server when it requests security | ||||
|     case ESP_GAP_BLE_SEC_REQ_EVT: | ||||
|       ESP_LOGV(TAG, "ESP_GAP_BLE_SEC_REQ_EVT %x", event); | ||||
|       esp_ble_gap_security_rsp(param->ble_security.ble_req.bd_addr, true); | ||||
|       break; | ||||
|     // This event is sent once authentication has completed | ||||
|     case ESP_GAP_BLE_AUTH_CMPL_EVT: | ||||
|       esp_bd_addr_t bd_addr; | ||||
|       memcpy(bd_addr, param->ble_security.auth_cmpl.bd_addr, sizeof(esp_bd_addr_t)); | ||||
|       ESP_LOGI(TAG, "auth complete. remote BD_ADDR: %s", format_hex(bd_addr, 6).c_str()); | ||||
|       if (!param->ble_security.auth_cmpl.success) { | ||||
|         ESP_LOGE(TAG, "auth fail reason = 0x%x", param->ble_security.auth_cmpl.fail_reason); | ||||
|       } else { | ||||
|         ESP_LOGV(TAG, "auth success. address type = %d auth mode = %d", param->ble_security.auth_cmpl.addr_type, | ||||
|                  param->ble_security.auth_cmpl.auth_mode); | ||||
|       } | ||||
|       break; | ||||
|     // There are other events we'll want to implement at some point to support things like pass key | ||||
|     // https://github.com/espressif/esp-idf/blob/cba69dd088344ed9d26739f04736ae7a37541b3a/examples/bluetooth/bluedroid/ble/gatt_security_client/tutorial/Gatt_Security_Client_Example_Walkthrough.md | ||||
|     default: | ||||
|       break; | ||||
|   } | ||||
| } | ||||
|  | ||||
| // Parse GATT values into a float for a sensor. | ||||
| // Ref: https://www.bluetooth.com/specifications/assigned-numbers/format-types/ | ||||
| float BLEClient::parse_char_value(uint8_t *value, uint16_t length) { | ||||
|   | ||||
| @@ -11,6 +11,7 @@ | ||||
| #include <esp_gap_ble_api.h> | ||||
| #include <esp_gattc_api.h> | ||||
| #include <esp_bt_defs.h> | ||||
| #include <esp_gatt_common_api.h> | ||||
|  | ||||
| namespace esphome { | ||||
| namespace ble_client { | ||||
| @@ -86,6 +87,7 @@ class BLEClient : public espbt::ESPBTClient, public Component { | ||||
|  | ||||
|   void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, | ||||
|                            esp_ble_gattc_cb_param_t *param) override; | ||||
|   void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override; | ||||
|   bool parse_device(const espbt::ESPBTDevice &device) override; | ||||
|   void on_scan_end() override {} | ||||
|   void connect() override; | ||||
|   | ||||
| @@ -262,6 +262,9 @@ void ESP32BLETracker::real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ | ||||
|     default: | ||||
|       break; | ||||
|   } | ||||
|   for (auto *client : global_esp32_ble_tracker->clients_) { | ||||
|     client->gap_event_handler(event, param); | ||||
|   } | ||||
| } | ||||
|  | ||||
| void ESP32BLETracker::gap_scan_set_param_complete_(const esp_ble_gap_cb_param_t::ble_scan_param_cmpl_evt_param ¶m) { | ||||
|   | ||||
| @@ -155,6 +155,7 @@ class ESPBTClient : public ESPBTDeviceListener { | ||||
|  public: | ||||
|   virtual void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, | ||||
|                                    esp_ble_gattc_cb_param_t *param) = 0; | ||||
|   virtual void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) = 0; | ||||
|   virtual void connect() = 0; | ||||
|   void set_state(ClientState st) { this->state_ = st; } | ||||
|   ClientState state() const { return state_; } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user