ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
bluetooth_connection.cpp
Go to the documentation of this file.
2
3#ifdef USE_ESP32
4#include <esp_gap_ble_api.h>
5#include <esp_gattc_api.h>
6#endif
7
8#ifdef USE_BLUETOOTH_PROXY_CONNECTIONS
9
11#include "esphome/core/log.h"
12
14
15static const char *const TAG = "bluetooth_connection";
16
17BatchClose close_service_batch(api::BluetoothGATTGetServicesResponse &resp, size_t &current_size, int16_t &send_service,
18 uint8_t connection_index, const char *address_str) {
19 // Calculate the actual size of just this service (+1 for the field tag)
20 size_t service_size = resp.services.back().calculate_size() + 1;
21
22 if (current_size + service_size > MAX_PACKET_SIZE) {
23 if (resp.services.size() > 1) {
24 // We would go over -- pop the last service and retry it in the next batch
25 resp.services.pop_back();
26 ESP_LOGD(TAG, "[%d] [%s] Service %d would exceed limit (current: %u + service: %u > %u), sending current batch",
27 connection_index, address_str, send_service, (unsigned) current_size, (unsigned) service_size,
28 (unsigned) MAX_PACKET_SIZE);
29 // Don't advance send_service -- the popped service goes into the next batch
30 } else {
31 // This single service is too large, but we have to send it anyway;
32 // advance so we don't get stuck
33 ESP_LOGW(TAG, "[%d] [%s] Service %d is too large (%u bytes) but sending anyway", connection_index, address_str,
34 send_service, (unsigned) service_size);
35 send_service++;
36 }
37 return BatchClose::SEND;
38 }
39
40 current_size += service_size;
41 send_service++;
43}
44
45} // namespace esphome::bluetooth_connection
46
47#endif // USE_BLUETOOTH_PROXY_CONNECTIONS
48
49#if defined(USE_ESP32) && defined(USE_BLE_GATT_CLIENT)
51
52// Address-scoped Bluedroid maintenance. Gated with the connection surface:
53// the advertisement-only arm no longer dispatches these requests at all.
54
56 esp_bd_addr_t bda;
58 return esp_ble_remove_bond_device(bda);
59}
60
62 esp_bd_addr_t bda;
64 return esp_ble_gattc_cache_clean(bda);
65}
66
67} // namespace esphome::bluetooth_connection
68#endif // USE_ESP32 && USE_BLE_GATT_CLIENT
uint8_t address
Definition bl0906.h:4
std::vector< BluetoothGATTService > services
Definition api_pb2.h:2108
void uint64_to_mac_msb_first(uint64_t address, uint8_t out[6])
Unpack a uint64 BLE address into printable (MSB-first) byte order — the order bd_addr_t / esp_bd_addr...
Definition ble_device.h:169
conn_err_t unpair_device(uint64_t address)
BatchClose
Result of close_service_batch: keep filling the batch or send it now.
BatchClose close_service_batch(api::BluetoothGATTGetServicesResponse &resp, size_t &current_size, int16_t &send_service, uint8_t connection_index, const char *address_str)
Close out the service just packed into resp (account its actual wire size, advance the cursor) and de...
conn_err_t clear_gatt_cache(uint64_t address)