ESPHome 2026.10.0-dev
Loading...
Searching...
No Matches
ble_scanner.h
Go to the documentation of this file.
1#pragma once
2
3#include <cinttypes>
4#include <cstdio>
5#include <ctime>
6
12
13// No platform #ifdef: ble_device_base provides the BLE types on every platform,
14// and this component is only compiled when configured — which requires a BLE
15// hub — so it builds on any platform with a BLEHub tracker without a per-chip
16// guard.
17namespace esphome::ble_scanner {
18
20 public:
21 bool parse_device(const ble_device_base::ESPBTDevice &device) override {
22 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
23 // Escape special characters in the device name for valid JSON. Control characters stay in the \u00XX form this
24 // sensor has always published.
25 char escaped_name[128];
26 json_escape_into_buffer(escaped_name, device.get_name(), /*short_control_escapes=*/false);
27
28 char buf[256];
29 snprintf(buf, sizeof(buf), "{\"timestamp\":%" PRId64 ",\"address\":\"%s\",\"rssi\":%d,\"name\":\"%s\"}",
30 static_cast<int64_t>(::time(nullptr)), device.address_str_to(addr_buf), device.get_rssi(), escaped_name);
31 this->publish_state(buf);
32 return true;
33 }
34 void dump_config() override;
35};
36
37} // namespace esphome::ble_scanner
ESPDEPRECATED("Use address_str_to() instead. Removed in 2027.2.0.", "2026.8.0") std const char * address_str_to(char *buf) const
Return MAC as "XX:XX:XX:XX:XX:XX" string.
StringRef get_name() const
Advertised name as a view into the fixed buffer (always NUL-terminated, so c_str() is safe); converts...
Definition ble_device.h:221
bool parse_device(const ble_device_base::ESPBTDevice &device) override
Definition ble_scanner.h:21
void publish_state(const std::string &state)
Definition text_sensor.h:40
const char * json_escape_into_buffer(std::span< char > buf, StringRef value, bool short_control_escapes)
Copy value into buf, escaping the characters that cannot appear raw inside a JSON string literal.
Definition helpers.cpp:357