ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ble_advertising.cpp
Go to the documentation of this file.
1#include "ble_advertising.h"
2
3#ifdef USE_ESP32
4#ifdef USE_ESP32_BLE_ADVERTISING
5
6#include <algorithm>
7#include <cstring>
9#include "ble.h"
10#include "ble_uuid.h"
11#include "esphome/core/log.h"
13
14#ifndef CONFIG_BT_CONTROLLER_DISABLED
15#include <esp_bt.h>
16#endif
17
18namespace esphome::esp32_ble {
19
20using ble_device_base::AdvPayload;
22
23ESPHOME_LOG_TAG(TAG, "esp32_ble.advertising");
24
25// The TX power Bluedroid advertised, from its BTM_BLE_ADV_TX_POWER table
26static int8_t adv_tx_power_dbm() {
27#ifdef CONFIG_IDF_TARGET_ESP32
28 static constexpr int8_t TX_POWER_DBM[] = {-12, -9, -6, -3, 0, 3, 6, 9};
29#else
30 static constexpr int8_t TX_POWER_DBM[] = {-24, -21, -18, -15, -12, -9, -6, -3, 0, 3, 6, 9, 12, 15, 18, 20};
31#endif
32#ifdef CONFIG_BT_CONTROLLER_DISABLED
33 const size_t level = 0; // a hosted controller; Bluedroid sends level 0 too
34#else
35 const size_t level = esp_ble_tx_power_get(ESP_BLE_PWR_TYPE_ADV);
36#endif
37 return TX_POWER_DBM[std::min(level, std::size(TX_POWER_DBM) - 1)];
38}
39
40BLEAdvertising::BLEAdvertising(uint32_t advertising_cycle_time) : advertising_cycle_time_(advertising_cycle_time) {
41 this->advertising_params_.adv_int_min = 0x20;
42 this->advertising_params_.adv_int_max = 0x40;
43 this->advertising_params_.adv_type = ADV_TYPE_IND;
44 this->advertising_params_.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
45 this->advertising_params_.channel_map = ADV_CHNL_ALL;
46 this->advertising_params_.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY;
47 this->advertising_params_.peer_addr_type = BLE_ADDR_TYPE_PUBLIC;
48}
49
52 this->advertising_uuids_.erase(std::remove(this->advertising_uuids_.begin(), this->advertising_uuids_.end(), uuid),
53 this->advertising_uuids_.end());
54}
55
56void BLEAdvertising::set_service_data(std::span<const uint8_t> data) {
57 // Too long to fit is the same as none
58 this->service_data_len_ = data.size() <= MAX_DATA_LEN ? data.size() : 0;
59 memcpy(this->service_data_.data(), data.data(), this->service_data_len_);
60}
61
62void BLEAdvertising::set_service_data(const std::vector<uint8_t> &data) {
63 this->set_service_data(std::span<const uint8_t>(data));
64}
65
66void BLEAdvertising::set_manufacturer_data(const std::vector<uint8_t> &data) {
67 // Only what fits goes on air
68 this->manufacturer_data_len_ = std::min(data.size(), MAX_DATA_LEN);
69 memcpy(this->manufacturer_data_.data(), data.data(), this->manufacturer_data_len_);
70}
71
73 char name_buffer[ESP32BLE::DEVICE_NAME_BUFFER_SIZE];
74 const char *name = global_ble->device_name(name_buffer);
75 AdvPayload adv;
76 AdvPayload rsp;
77 build_adv_packets({.uuids = this->advertising_uuids_,
78 .priority_uuid = this->priority_uuid_,
79 .manufacturer_data = {this->manufacturer_data_.data(), this->manufacturer_data_len_},
80 .service_data = {this->service_data_.data(), this->service_data_len_},
81 .name = name,
82 .name_len = strlen(name),
83 .appearance = this->appearance_,
84 .tx_power_dbm = adv_tx_power_dbm(),
85 .name_in_adv = this->include_name_in_adv_},
86 adv, rsp);
87
88 // Bluedroid copies the bytes during each call
89 esp_err_t err = esp_ble_gap_config_adv_data_raw(const_cast<uint8_t *>(adv.data()), adv.size());
90 if (err != ESP_OK) {
91 ESP_LOGE(TAG, "esp_ble_gap_config_adv_data_raw failed: %s", esp_err_to_name(err));
92 return err;
93 }
94 err = esp_ble_gap_config_scan_rsp_data_raw(const_cast<uint8_t *>(rsp.data()), rsp.size());
95 if (err != ESP_OK) {
96 ESP_LOGE(TAG, "esp_ble_gap_config_scan_rsp_data_raw failed: %s", esp_err_to_name(err));
97 return err;
98 }
99 err = esp_ble_gap_start_advertising(&this->advertising_params_);
100 if (err != ESP_OK) {
101 ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %s", esp_err_to_name(err));
102 return err;
103 }
104 return ESP_OK;
105}
106
108 if (this->current_adv_index_ == -1) {
110 } else {
112 }
113}
114
116 this->start_pending_ = false;
117 esp_err_t err = esp_ble_gap_stop_advertising();
118 if (err != ESP_OK) {
119 ESP_LOGE(TAG, "esp_ble_gap_stop_advertising failed: %d", err);
120 return;
121 }
122 if (this->current_adv_index_ != -1) {
124 }
125}
126
128 // Send a queued start after the event drain
129 if (this->start_pending_) {
130 this->start_pending_ = false;
131 this->start();
132 }
133 if (this->raw_advertisements_callbacks_.empty()) {
134 return;
135 }
137 if (now - this->last_advertisement_time_ > this->advertising_cycle_time_) {
138 this->stop();
139 this->current_adv_index_ += 1;
140 if (static_cast<size_t>(this->current_adv_index_) >= this->raw_advertisements_callbacks_.size()) {
141 this->current_adv_index_ = -1;
142 }
143 this->start();
144 this->last_advertisement_time_ = now;
145 }
146}
147
148void BLEAdvertising::register_raw_advertisement_callback(std::function<void(bool)> &&callback) {
149 this->raw_advertisements_callbacks_.push_back(std::move(callback));
150}
151
152} // namespace esphome::esp32_ble
153
154#endif // USE_ESP32_BLE_ADVERTISING
155#endif // USE_ESP32
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
A 31 byte advertising or scan response payload, packed in the order and with the fit rules of Bluedro...
Definition adv_payload.h:17
const uint8_t * data() const
Definition adv_payload.h:21
void set_manufacturer_data(const std::vector< uint8_t > &data)
std::vector< ESPBTUUID > advertising_uuids_
std::array< uint8_t, MAX_DATA_LEN > service_data_
std::vector< std::function< void(bool)> > raw_advertisements_callbacks_
BLEAdvertising(uint32_t advertising_cycle_time)
static constexpr size_t MAX_DATA_LEN
esp_ble_adv_params_t advertising_params_
void set_service_data(const std::vector< uint8_t > &data)
std::array< uint8_t, MAX_DATA_LEN > manufacturer_data_
void register_raw_advertisement_callback(std::function< void(bool)> &&callback)
static constexpr size_t DEVICE_NAME_BUFFER_SIZE
Definition ble.h:116
const char * device_name(char *buffer) const
The device name; written to buffer unless the configured name is used as is.
Definition ble.cpp:275
void build_adv_packets(const AdvContent &content, AdvPayload &adv, AdvPayload &rsp)
Split content between the advertising packet and the scan response.
ESP32BLE * global_ble
Definition ble.cpp:908
ESPHOME_LOG_TAG(TAG, "esp32_ble")
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t