ESPHome 2025.5.0
Loading...
Searching...
No Matches
ble_client_base.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
7
8#include "ble_service.h"
9
10#include <array>
11#include <string>
12#include <vector>
13
14#include <esp_bt_defs.h>
15#include <esp_gap_ble_api.h>
16#include <esp_gatt_common_api.h>
17#include <esp_gattc_api.h>
18
19namespace esphome {
20namespace esp32_ble_client {
21
23
24static const int UNSET_CONN_ID = 0xFFFF;
25
27 public:
28 void setup() override;
29 void loop() override;
30 float get_setup_priority() const override;
31 void dump_config() override;
32
33 void run_later(std::function<void()> &&f); // NOLINT
34 bool parse_device(const espbt::ESPBTDevice &device) override;
35 void on_scan_end() override {}
36 bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
37 esp_ble_gattc_cb_param_t *param) override;
38 void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
39 void connect() override;
40 esp_err_t pair();
41 void disconnect() override;
43 void release_services();
44
45 bool connected() { return this->state_ == espbt::ClientState::ESTABLISHED; }
46
47 void set_auto_connect(bool auto_connect) { this->auto_connect_ = auto_connect; }
48
49 void set_address(uint64_t address) {
50 this->address_ = address;
51 this->remote_bda_[0] = (address >> 40) & 0xFF;
52 this->remote_bda_[1] = (address >> 32) & 0xFF;
53 this->remote_bda_[2] = (address >> 24) & 0xFF;
54 this->remote_bda_[3] = (address >> 16) & 0xFF;
55 this->remote_bda_[4] = (address >> 8) & 0xFF;
56 this->remote_bda_[5] = (address >> 0) & 0xFF;
57 if (address == 0) {
58 this->address_str_ = "";
59 } else {
60 this->address_str_ =
61 str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, (uint8_t) (this->address_ >> 40) & 0xff,
62 (uint8_t) (this->address_ >> 32) & 0xff, (uint8_t) (this->address_ >> 24) & 0xff,
63 (uint8_t) (this->address_ >> 16) & 0xff, (uint8_t) (this->address_ >> 8) & 0xff,
64 (uint8_t) (this->address_ >> 0) & 0xff);
65 }
66 }
67 std::string address_str() const { return this->address_str_; }
68
70 BLEService *get_service(uint16_t uuid);
72 BLECharacteristic *get_characteristic(uint16_t service, uint16_t chr);
73 BLECharacteristic *get_characteristic(uint16_t handle);
75 BLEDescriptor *get_descriptor(uint16_t service, uint16_t chr, uint16_t descr);
76 BLEDescriptor *get_descriptor(uint16_t handle);
77 // Get the configuration descriptor for the given characteristic handle.
78 BLEDescriptor *get_config_descriptor(uint16_t handle);
79
80 float parse_char_value(uint8_t *value, uint16_t length);
81
82 int get_gattc_if() const { return this->gattc_if_; }
83 uint8_t *get_remote_bda() { return this->remote_bda_; }
84 esp_ble_addr_type_t get_remote_addr_type() const { return this->remote_addr_type_; }
85 void set_remote_addr_type(esp_ble_addr_type_t address_type) { this->remote_addr_type_ = address_type; }
86 uint16_t get_conn_id() const { return this->conn_id_; }
87 uint64_t get_address() const { return this->address_; }
88 bool is_paired() const { return this->paired_; }
89
90 uint8_t get_connection_index() const { return this->connection_index_; }
91
93
94 bool check_addr(esp_bd_addr_t &addr) { return memcmp(addr, this->remote_bda_, sizeof(esp_bd_addr_t)) == 0; }
95
96 protected:
98 esp_bd_addr_t remote_bda_;
99 esp_ble_addr_type_t remote_addr_type_{BLE_ADDR_TYPE_PUBLIC};
100 uint16_t conn_id_{UNSET_CONN_ID};
101 uint64_t address_{0};
102 bool auto_connect_{false};
103 std::string address_str_{};
105 int16_t service_count_{0};
106 uint16_t mtu_{23};
107 bool paired_{false};
108 espbt::ConnectionType connection_type_{espbt::ConnectionType::V1};
109 std::vector<BLEService *> services_;
110 esp_gatt_status_t status_{ESP_GATT_OK};
111
112 void log_event_(const char *name);
113};
114
115} // namespace esp32_ble_client
116} // namespace esphome
117
118#endif // USE_ESP32
uint8_t address
Definition bl0906.h:4
std::vector< BLEService * > services_
BLEDescriptor * get_descriptor(espbt::ESPBTUUID service, espbt::ESPBTUUID chr, espbt::ESPBTUUID descr)
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
BLECharacteristic * get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr)
esp_ble_addr_type_t get_remote_addr_type() const
void run_later(std::function< void()> &&f)
BLEService * get_service(espbt::ESPBTUUID uuid)
bool gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param) override
bool parse_device(const espbt::ESPBTDevice &device) override
virtual void set_connection_type(espbt::ConnectionType ct)
float parse_char_value(uint8_t *value, uint16_t length)
BLEDescriptor * get_config_descriptor(uint16_t handle)
void set_remote_addr_type(esp_ble_addr_type_t address_type)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:309
uint16_t length
Definition tt21100.cpp:0