ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
ble_presence_device.h
Go to the documentation of this file.
1#pragma once
2
6
7// No platform #ifdef: ble_device_base provides the BLE types on every platform,
8// and this component is only compiled when configured — which requires a BLE
9// hub — so it builds on any platform with a BLEHub tracker without a per-chip
10// guard.
11namespace esphome::ble_presence {
12
15 public Component {
16 public:
17 void set_address(uint64_t address) {
19 this->address_ = address;
20 }
21 void set_irk(uint8_t *irk) {
22 this->match_by_ = MATCH_BY_IRK;
23 this->irk_ = irk;
24 }
33 void set_service_uuid128(uint8_t *uuid) {
36 }
41 void set_ibeacon_major(uint16_t major) {
42 this->check_ibeacon_major_ = true;
43 this->ibeacon_major_ = major;
44 }
45 void set_ibeacon_minor(uint16_t minor) {
46 this->check_ibeacon_minor_ = true;
47 this->ibeacon_minor_ = minor;
48 }
49 void set_minimum_rssi(int rssi) {
50 this->check_minimum_rssi_ = true;
51 this->minimum_rssi_ = rssi;
52 }
53 void set_timeout(uint32_t timeout) { this->timeout_ = timeout; }
54 bool parse_device(const ble_device_base::ESPBTDevice &device) override {
55 if (this->check_minimum_rssi_ && this->minimum_rssi_ > device.get_rssi()) {
56 return false;
57 }
58 switch (this->match_by_) {
60 if (device.address_uint64() == this->address_) {
61 this->set_found_(true);
62 return true;
63 }
64 break;
65 case MATCH_BY_IRK:
66 if (device.resolve_irk(this->irk_)) {
67 this->set_found_(true);
68 return true;
69 }
70 break;
72 for (auto uuid : device.get_service_uuids()) {
73 if (this->uuid_ == uuid) {
74 this->set_found_(true);
75 return true;
76 }
77 }
78 break;
80 auto maybe_ibeacon = device.get_ibeacon();
81 if (!maybe_ibeacon.has_value()) {
82 return false;
83 }
84
85 auto ibeacon = *maybe_ibeacon;
86
87 if (this->ibeacon_uuid_ != ibeacon.get_uuid()) {
88 return false;
89 }
90
91 if (this->check_ibeacon_major_ && this->ibeacon_major_ != ibeacon.get_major()) {
92 return false;
93 }
94
95 if (this->check_ibeacon_minor_ && this->ibeacon_minor_ != ibeacon.get_minor()) {
96 return false;
97 }
98
99 this->set_found_(true);
100 return true;
101 }
102 return false;
103 }
104
105 void loop() override {
106 if (this->found_ && millis() - this->last_seen_ > this->timeout_)
107 this->set_found_(false);
108 }
109 void dump_config() override;
110
111 protected:
112 void set_found_(bool state) {
113 this->found_ = state;
114 if (state)
115 this->last_seen_ = millis();
116 this->publish_state(state);
117 }
120
121 uint64_t address_;
122 uint8_t *irk_;
123
125
127 uint16_t ibeacon_major_{0};
128 uint16_t ibeacon_minor_{0};
129
131
135
136 bool found_{false};
139};
140
141} // namespace esphome::ble_presence
uint8_t address
Definition bl0906.h:4
void publish_state(bool new_state)
Publish a new state to the front-end.
bool state
The current state of this binary sensor. Also used as the backing storage for StatefulEntityBase.
const std::vector< ESPBTUUID > & get_service_uuids() const
Definition ble_device.h:223
optional< ESPBLEiBeacon > get_ibeacon() const
uint64_t address_uint64() const
Return MAC as packed uint64 (byte 0 in LSB — matches esp32's address_uint64).
bool resolve_irk(const uint8_t *irk) const
Resolve a Resolvable Private Address against a 16-byte IRK (Bluetooth "ah" function,...
static ESPBTUUID from_uint16(uint16_t uuid)
static ESPBTUUID from_raw(const uint8_t *data)
Construct from raw 16-byte little-endian UUID.
static ESPBTUUID from_uint32(uint32_t uuid)
bool parse_device(const ble_device_base::ESPBTDevice &device) override
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
static void uint32_t