ESPHome
2026.10.0-dev
Loading...
Searching...
No Matches
esphome
components
ble_scanner
ble_scanner.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <cinttypes>
4
#include <cstdio>
5
#include <ctime>
6
7
#include "
esphome/core/component.h
"
8
#include "
esphome/core/helpers.h
"
9
#include "
esphome/core/string_ref.h
"
10
#include "
esphome/components/ble_device_base/ble_device.h
"
11
#include "
esphome/components/text_sensor/text_sensor.h
"
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.
17
namespace
esphome::ble_scanner
{
18
19
class
BLEScanner
final :
public
text_sensor::TextSensor
,
public
ble_device_base::ESPBTDeviceListener
,
public
Component
{
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
ble_device.h
esphome::Component
Definition
component.h:145
esphome::ble_device_base::ESPBTDevice
Definition
ble_device.h:178
esphome::ble_device_base::ESPBTDevice::address_str_to
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.
Definition
ble_device.cpp:418
esphome::ble_device_base::ESPBTDevice::get_name
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
esphome::ble_device_base::ESPBTDevice::get_rssi
int get_rssi() const
Definition
ble_device.h:218
esphome::ble_device_base::ESPBTDeviceListener
Definition
ble_device.h:284
esphome::ble_scanner::BLEScanner
Definition
ble_scanner.h:19
esphome::ble_scanner::BLEScanner::dump_config
void dump_config() override
Definition
ble_scanner.cpp:8
esphome::ble_scanner::BLEScanner::parse_device
bool parse_device(const ble_device_base::ESPBTDevice &device) override
Definition
ble_scanner.h:21
esphome::text_sensor::TextSensor
Definition
text_sensor.h:28
esphome::text_sensor::TextSensor::publish_state
void publish_state(const std::string &state)
Definition
text_sensor.h:40
component.h
helpers.h
esphome::ble_scanner
Definition
ble_scanner.cpp:4
esphome::json_escape_into_buffer
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
string_ref.h
text_sensor.h
Generated by
1.12.0