ESPHome 2026.1.5
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
10
11#ifdef USE_ESP32
12
13namespace esphome {
14namespace ble_scanner {
15
17 public:
18 bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override {
19 // Format JSON using stack buffer to avoid heap allocations from string concatenation
20 char buf[128];
21 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
22 snprintf(buf, sizeof(buf), "{\"timestamp\":%" PRId64 ",\"address\":\"%s\",\"rssi\":%d,\"name\":\"%s\"}",
23 static_cast<int64_t>(::time(nullptr)), device.address_str_to(addr_buf), device.get_rssi(),
24 device.get_name().c_str());
25 this->publish_state(buf);
26 return true;
27 }
28 void dump_config() override;
29};
30
31} // namespace ble_scanner
32} // namespace esphome
33
34#endif
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
Definition ble_scanner.h:18
const char * address_str_to(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf) const
Format MAC address into provided buffer, returns pointer to buffer for convenience.
void publish_state(const std::string &state)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7