ESPHome 2026.8.0b4
Loading...
Searching...
No Matches
ble_client_state.h
Go to the documentation of this file.
1// ble_client_state.h
2//
3// Platform-neutral GATT client connection state types, shared by every
4// platform's GATT client implementation (esp32_ble_client, bluetooth_connection
5// backends). Moved here from esp32_ble_tracker, which re-exports them under its
6// own namespace for backward compatibility.
7
8#pragma once
9
10#include <cstdint>
11
13
18static constexpr int GATT_ERR_NOT_CONNECTED = -1;
19static constexpr int GATT_ERR_NO_MEMORY = -2;
22static constexpr int GATT_ERR_UNLIKELY = 0x0E;
23
26static constexpr uint32_t GATT_DISCONNECT_TIMEOUT_MS = 10000;
27
29static constexpr uint16_t DEFAULT_ATT_MTU = 23;
30
31// Preferred connection parameters shared by every platform's GATT client so
32// the backends cannot drift (units: interval 1.25 ms, timeout 10 ms; latency
33// 0). FAST covers connection setup and service discovery; MEDIUM is the
34// steady state once established. Stack defaults (12.5-15 ms) are too slow for
35// stable connections through WiFi-based BLE proxies, causing disconnections;
36// MEDIUM balances responsiveness with bandwidth usage.
37static constexpr uint16_t MEDIUM_MIN_CONN_INTERVAL = 0x07; // 7 * 1.25ms = 8.75ms
38static constexpr uint16_t MEDIUM_MAX_CONN_INTERVAL = 0x09; // 9 * 1.25ms = 11.25ms
39// The timeout value was increased from 6s to 8s to address stability issues observed
40// in certain BLE devices when operating through WiFi-based BLE proxies. The longer
41// timeout reduces the likelihood of disconnections during periods of high latency.
42static constexpr uint16_t MEDIUM_CONN_TIMEOUT = 800; // 800 * 10ms = 8s
43
44// Fastest connection parameters for devices with short discovery timeouts
45static constexpr uint16_t FAST_MIN_CONN_INTERVAL = 0x06; // 6 * 1.25ms = 7.5ms (BLE minimum)
46static constexpr uint16_t FAST_MAX_CONN_INTERVAL = 0x06; // 6 * 1.25ms = 7.5ms
47static constexpr uint16_t FAST_CONN_TIMEOUT = 1000; // 1000 * 10ms = 10s
48
49enum class ClientState : uint8_t {
50 // Connection is allocated
51 INIT,
52 // Client is disconnecting
54 // Connection is idle, no device detected.
55 IDLE,
56 // Device advertisement found.
58 // Connection in progress.
60 // Initial connection established.
62 // The client and sub-clients have completed setup.
64};
65
66// Helper function to convert ClientState to string
67const char *client_state_to_string(ClientState state);
68
69enum class ConnectionType : uint8_t {
70 // The default connection type, we hold all the services in ram
71 // for the duration of the connection.
72 V1,
73 // The client has a cache of the services and mtu so we should not
74 // fetch them again
76 // The client does not need the services and mtu once we send them
77 // so we should wipe them from memory as soon as we send them
79};
80
81} // namespace esphome::ble_device_base
bool state
Definition fan.h:2
const char * client_state_to_string(ClientState state)
static void uint32_t