ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
improv_ble_component.cpp
Go to the documentation of this file.
2
3#include <array>
4
7#include "esphome/core/log.h"
8
9#ifdef USE_PROVISIONING
11#endif
12
13#ifdef USE_ESP32
14
18
20
21using namespace bytebuffer;
22
23ESPHOME_LOG_TAG(TAG, "improv_ble.component");
24
25static constexpr uint32_t STOP_SERVICE_TIMEOUT_ID = 0;
26static constexpr uint32_t WIFI_CONNECT_TIMEOUT_ID = 1;
27
28static constexpr size_t IMPROV_MAX_LOG_BYTES = 128;
29static constexpr char ESPHOME_MY_LINK[] = "https://my.home-assistant.io/redirect/config_flow_start?domain=esphome";
30// command + data length + trailing byte
31static constexpr size_t RPC_RESPONSE_OVERHEAD = 3;
32// Reserves the ESPHOME_MY_LINK entry; a maximal next URL displaces only the
33// lower value web server URL
34static constexpr size_t MAX_NEXT_URL_LEN =
35 improv::RPC_RESPONSE_MAX_SIZE - RPC_RESPONSE_OVERHEAD - 1 - sizeof(ESPHOME_MY_LINK);
36static constexpr uint16_t STOP_ADVERTISING_DELAY =
37 10000; // Delay (ms) before stopping service to allow BLE clients to read the final state
38static constexpr uint16_t NAME_ADVERTISING_INTERVAL = 60000; // Advertise name every 60 seconds
39static constexpr uint16_t NAME_ADVERTISING_DURATION = 1000; // Advertise name for 1 second
40
41// Improv service data constants
42static constexpr uint8_t IMPROV_SERVICE_DATA_SIZE = 8;
43static constexpr uint8_t IMPROV_PROTOCOL_ID_1 = 0x77; // 'P' << 1 | 'R' >> 7
44static constexpr uint8_t IMPROV_PROTOCOL_ID_2 = 0x46; // 'I' << 1 | 'M' >> 7
45
46ImprovBLEComponent::ImprovBLEComponent() { global_improv_component = this; }
47
48static bool provisioning_closed() {
49#ifdef USE_PROVISIONING
51#else
52 return false;
53#endif
54}
55
56void ImprovBLEComponent::setup() {
57#ifdef USE_BINARY_SENSOR
58 if (this->authorizer_binary_sensor_ != nullptr) {
59 this->authorizer_binary_sensor_->add_on_state_callback([this](bool state) {
60 if (state) {
61 this->authorized_start_ = millis();
62 this->identify_start_ = 0;
63 }
64 });
65 }
66#endif
67 global_ble_server->on_disconnect([this](uint16_t conn_id) { this->set_error_(improv::ERROR_NONE); });
68
69#ifdef USE_PROVISIONING
72 ESP_LOGD(TAG, "Provisioning window closed; stopping Improv");
73 this->stop();
74 });
75 }
76#endif
77
78 // Start with loop disabled - will be enabled by start() when needed
79 this->disable_loop();
80}
81
82void ImprovBLEComponent::setup_characteristics() {
85 BLEDescriptor *status_descriptor = new BLE2902();
86 this->status_->add_descriptor(status_descriptor);
87
90 BLEDescriptor *error_descriptor = new BLE2902();
91 this->error_->add_descriptor(error_descriptor);
92
93 auto *rpc = this->service_->create_characteristic(improv::RPC_COMMAND_UUID, BLECharacteristic::PROPERTY_WRITE);
94 rpc->on_write([this](std::span<const uint8_t> data, uint16_t id) {
95 if (!data.empty()) {
96 this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
97 }
98 });
99 BLEDescriptor *rpc_descriptor = new BLE2902();
100 rpc->add_descriptor(rpc_descriptor);
101
104 BLEDescriptor *rpc_response_descriptor = new BLE2902();
105 this->rpc_response_->add_descriptor(rpc_response_descriptor);
106
107 auto *capabilities_chr =
108 this->service_->create_characteristic(improv::CAPABILITIES_UUID, BLECharacteristic::PROPERTY_READ);
109 BLEDescriptor *capabilities_descriptor = new BLE2902();
110 capabilities_chr->add_descriptor(capabilities_descriptor);
111 uint8_t capabilities = 0x00;
112#ifdef USE_OUTPUT
113 if (this->status_indicator_ != nullptr)
114 capabilities |= improv::CAPABILITY_IDENTIFY;
115#endif
116 capabilities_chr->set_value(ByteBuffer::wrap(capabilities));
117}
118
119void ImprovBLEComponent::loop() {
120 // A Wi-Fi attempt outlives the BLE session that started it
121 this->save_connecting_sta_();
123 if (this->state_ != improv::STATE_STOPPED) {
124 this->cancel_timeout(STOP_SERVICE_TIMEOUT_ID);
125 // Offered again after ble.enable, like the service the server restarts, unless stop() cleared its flag
126 this->should_start_ = this->service_->should_start();
127 this->state_ = improv::STATE_STOPPED;
128 // Turned off here: the STOPPED branch below is skipped while BLE is off
129 this->set_status_indicator_state_(false);
130#ifdef USE_IMPROV_BLE_STATE_CALLBACK
131 this->state_callback_.call(this->state_, this->error_state_);
132#endif
133 this->release_advertising_();
134 }
135 if (!this->should_start_)
136 this->finish_stop_();
137 // Also drops a partial command left from a session that stop() ended
138 this->incoming_data_.clear();
139 return;
140 }
141 if (this->service_ == nullptr && this->should_start_) {
142 // Setup the service
143 ESP_LOGD(TAG, "Creating Improv service");
144 this->service_ = global_ble_server->create_service(ESPBTUUID::from_raw(improv::SERVICE_UUID), true);
145 this->setup_characteristics();
146 }
147
148 if (!this->incoming_data_.empty())
151
152 // Check if we need to update advertising type
153 if (this->state_ != improv::STATE_STOPPED && this->state_ != improv::STATE_PROVISIONED) {
155 }
156
157 switch (this->state_) {
158 case improv::STATE_STOPPED:
159 this->set_status_indicator_state_(false);
160
161 if (this->should_start_) {
162 // A service that an earlier session stopped stays STOPPED and can be started again
163 if (this->service_->is_created() || this->service_->is_stopped()) {
164 this->service_->start();
165 } else if (this->service_->is_running()) {
166 // A stop() that landed while the service was starting cleared this; ble.enable must offer it again
167 this->service_->set_should_start(true);
168 // Start by advertising the device name first BEFORE setting any state
169 ESP_LOGV(TAG, "Starting with device name advertising");
170 this->advertising_device_name_ = true;
172 // Set the payload before requesting, so advertising starts exactly once
173 esp32_ble::global_ble->advertising_set_service_data_and_name(std::span<const uint8_t>{}, true);
174 this->request_advertising_();
175
176 // Set initial state based on whether we have an authorizer
177 this->set_state_(this->get_initial_state_(), false);
178 this->set_error_(improv::ERROR_NONE);
179 this->should_start_ = false; // Clear flag after starting
180 ESP_LOGD(TAG, "Service started!");
181 }
182 } else {
183 this->finish_stop_();
184 }
185 break;
186 case improv::STATE_AWAITING_AUTHORIZATION: {
187#ifdef USE_BINARY_SENSOR
188 if (this->authorizer_binary_sensor_ == nullptr ||
189 (this->authorized_start_ != 0 && ((now - this->authorized_start_) < this->authorized_duration_))) {
190 this->set_state_(improv::STATE_AUTHORIZED);
191 } else {
192 if (!this->check_identify_())
193 this->set_status_indicator_state_(true);
194 }
195#else
196 this->set_state_(improv::STATE_AUTHORIZED);
197#endif
199 break;
200 }
201 case improv::STATE_AUTHORIZED: {
202#ifdef USE_BINARY_SENSOR
203 if (this->authorizer_binary_sensor_ != nullptr && now - this->authorized_start_ > this->authorized_duration_) {
204 ESP_LOGD(TAG, "Authorization timeout");
205 this->set_state_(improv::STATE_AWAITING_AUTHORIZATION);
206 return;
207 }
208#endif
209 if (!this->check_identify_()) {
210 this->set_status_indicator_state_((now % 1000) < 500);
211 }
213 break;
214 }
215 case improv::STATE_PROVISIONING: {
216 this->set_status_indicator_state_((now % 200) < 100);
218 break;
219 }
220 case improv::STATE_PROVISIONED: {
221 this->incoming_data_.clear();
222 this->set_status_indicator_state_(false);
223 // Provisioning complete, no further loop execution needed
224 this->disable_loop();
225 break;
226 }
227 }
228}
229
231#ifdef USE_OUTPUT
232 if (this->status_indicator_ == nullptr)
233 return;
234 if (this->status_indicator_state_ == state)
235 return;
237 this->status_indicator_->set_state(state);
238#endif
239}
240
241#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
242const char *ImprovBLEComponent::state_to_string_(improv::State state) {
243 switch (state) {
244 case improv::STATE_STOPPED:
245 return "STOPPED";
246 case improv::STATE_AWAITING_AUTHORIZATION:
247 return "AWAITING_AUTHORIZATION";
248 case improv::STATE_AUTHORIZED:
249 return "AUTHORIZED";
250 case improv::STATE_PROVISIONING:
251 return "PROVISIONING";
252 case improv::STATE_PROVISIONED:
253 return "PROVISIONED";
254 default:
255 return "UNKNOWN";
256 }
257}
258#endif
259
261 uint32_t now = millis();
262
263 bool identify = this->identify_start_ != 0 && now - this->identify_start_ <= this->identify_duration_;
264
265 if (identify) {
266 uint32_t time = now % 1000;
267 this->set_status_indicator_state_(time < 600 && time % 200 < 100);
268 }
269 return identify;
270}
271
272void ImprovBLEComponent::set_state_(improv::State state, bool update_advertising) {
273 // Skip if state hasn't changed
274 if (this->state_ == state) {
275 return;
276 }
277
278#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
279 ESP_LOGD(TAG, "State transition: %s (0x%02X) -> %s (0x%02X)", this->state_to_string_(this->state_), this->state_,
280 this->state_to_string_(state), state);
281#endif
282 this->state_ = state;
283 if (this->status_ != nullptr && (this->status_->get_value().empty() || this->status_->get_value()[0] != state)) {
284 this->status_->set_value(ByteBuffer::wrap(static_cast<uint8_t>(state)));
285 if (state != improv::STATE_STOPPED)
286 this->status_->notify();
287 }
288 // Only advertise valid Improv states (0x01-0x04).
289 // STATE_STOPPED (0x00) is internal only and not part of the Improv spec.
290 // Advertising 0x00 causes undefined behavior in some clients and makes them
291 // repeatedly connect trying to determine the actual state.
292 if (state != improv::STATE_STOPPED && update_advertising) {
293 // State change always overrides name advertising and resets the timer
294 this->advertising_device_name_ = false;
295 // Reset the timer so we wait another 60 seconds before advertising name
297 // Advertise the new state via service data
299 }
300#ifdef USE_IMPROV_BLE_STATE_CALLBACK
301 this->state_callback_.call(this->state_, this->error_state_);
302#endif
303}
304
305void ImprovBLEComponent::set_error_(improv::Error error) {
306 if (error != improv::ERROR_NONE) {
307 ESP_LOGE(TAG, "Error: %d", error);
308 }
309 // Null before the service is created and after stop() removes it; disconnects still arrive
310 if (this->error_ != nullptr && (this->error_->get_value().empty() || this->error_->get_value()[0] != error)) {
311 this->error_->set_value(ByteBuffer::wrap(static_cast<uint8_t>(error)));
312 if (this->state_ != improv::STATE_STOPPED)
313 this->error_->notify();
314 }
315}
316
317void ImprovBLEComponent::send_response_(std::span<const uint8_t> response) {
318 // The BLE characteristic owns its value, so one exact-size copy is required here
319 this->rpc_response_->set_value(std::vector<uint8_t>(response.begin(), response.end()));
320 if (this->state_ != improv::STATE_STOPPED)
321 this->rpc_response_->notify();
322}
323
324void ImprovBLEComponent::start() {
325 if (this->should_start_ || this->state_ != improv::STATE_STOPPED)
326 return;
327
328 // Don't (re)start advertising once the provisioning window has closed - e.g. when
329 // wifi tries to restart Improv after the window expired at runtime.
330 if (provisioning_closed()) {
331 ESP_LOGD(TAG, "Provisioning window closed; not starting Improv");
332 return;
333 }
334
335 ESP_LOGD(TAG, "Setting Improv to start");
336 this->should_start_ = true;
337 this->enable_loop();
338}
339
340void ImprovBLEComponent::stop() {
341 this->should_start_ = false;
342 // Also keeps the server from starting the service again after ble.enable
343 if (this->service_ != nullptr)
344 this->service_->set_should_start(false);
345 if (this->state_ == improv::STATE_STOPPED) {
346 // loop() removes a service that is left over
347 this->enable_loop();
348 return;
349 }
350 // Wait before stopping the service to ensure all BLE clients see the state change.
351 // This prevents clients from repeatedly reconnecting and wasting resources by allowing
352 // them to observe that the device is provisioned before the service disappears.
353 this->set_timeout(STOP_SERVICE_TIMEOUT_ID, STOP_ADVERTISING_DELAY, [this] {
354 // Release first so removing the service UUID does not restart advertising on the way out
355 this->release_advertising_();
356 // Stopped first, so a stop() from a state callback or log listener sees no active state without a service
357 this->set_state_(improv::STATE_STOPPED);
358 // loop() frees it through finish_stop_(), which waits if ble.enable is re-creating it
359 this->enable_loop();
360 });
361}
362
364 if (this->service_ != nullptr) {
365 // Not while the stack is creating it, also during ble.enable: it has no handle to delete yet
366 if (!this->service_->is_creating())
367 this->remove_service_();
368 } else if (this->connecting_sta_.get_ssid().empty()) {
369 // Idle until start(); a pending Wi-Fi attempt still needs saving first
370 this->disable_loop();
371 }
372}
373
375 if (this->service_ == nullptr)
376 return;
377 // Frees the characteristics and descriptors too; the next start() creates them again
378 global_ble_server->remove_service(this->service_->get_uuid(), this->service_->get_inst_id());
379 this->service_ = nullptr;
380 this->status_ = nullptr;
381 this->error_ = nullptr;
382 this->rpc_response_ = nullptr;
383}
384
385float ImprovBLEComponent::get_setup_priority() const { return setup_priority::AFTER_BLUETOOTH; }
386
387void ImprovBLEComponent::dump_config() {
388 ESP_LOGCONFIG(TAG, "Improv BLE:");
389#ifdef USE_BINARY_SENSOR
390 LOG_BINARY_SENSOR(" ", "Authorizer", this->authorizer_binary_sensor_);
391#endif
392#ifdef USE_OUTPUT
393 ESP_LOGCONFIG(TAG, " Status Indicator: '%s'", YESNO(this->status_indicator_ != nullptr));
394#endif
395}
396
398 if (this->incoming_data_.size() < 3)
399 return;
400 uint8_t length = this->incoming_data_[1];
401
402 ESP_LOGV_HEX(TAG, this->incoming_data_.data(), this->incoming_data_.size(), IMPROV_MAX_LOG_BYTES,
403 "Processing bytes - %s");
404 if (this->incoming_data_.size() - 3 == length) {
405 this->set_error_(improv::ERROR_NONE);
406 improv::ImprovCommand command = improv::parse_improv_data(this->incoming_data_);
407 switch (command.command) {
408 case improv::BAD_CHECKSUM:
409 ESP_LOGW(TAG, "Error decoding Improv payload");
410 this->set_error_(improv::ERROR_INVALID_RPC);
411 this->incoming_data_.clear();
412 break;
413 case improv::WIFI_SETTINGS: {
414 if (this->state_ != improv::STATE_AUTHORIZED) {
415 ESP_LOGW(TAG, "Settings received, but not authorized");
416 this->set_error_(improv::ERROR_NOT_AUTHORIZED);
417 this->incoming_data_.clear();
418 return;
419 }
420 if (provisioning_closed()) {
421 ESP_LOGW(TAG, "Provisioning window closed; refusing settings");
422 this->set_error_(improv::ERROR_NOT_AUTHORIZED);
423 this->incoming_data_.clear();
424 return;
425 }
426 if (wifi::global_wifi_component->is_disabled()) {
427 // Wi-Fi is disabled, so we can't provision. Respond immediately
428 // instead of letting the client wait out its provisioning timeout.
429 ESP_LOGW(TAG, "Wi-Fi is disabled; cannot provision");
430 this->set_error_(improv::ERROR_UNABLE_TO_CONNECT);
431 this->incoming_data_.clear();
432 return;
433 }
434 wifi::WiFiAP sta{};
435 sta.set_ssid(command.ssid.c_str());
436 sta.set_password(command.password.c_str());
437 this->connecting_sta_ = sta;
438
441 this->set_state_(improv::STATE_PROVISIONING);
442 ESP_LOGD(TAG, "Received Improv Wi-Fi settings ssid=%s, password=" LOG_SECRET("%s"), command.ssid.c_str(),
443 command.password.c_str());
444
445 this->set_timeout(WIFI_CONNECT_TIMEOUT_ID, 30000, [this]() { this->on_wifi_connect_timeout_(); });
446 this->incoming_data_.clear();
447 break;
448 }
449 case improv::IDENTIFY:
450 this->incoming_data_.clear();
451 this->identify_start_ = millis();
452 break;
453 default:
454 ESP_LOGW(TAG, "Unknown Improv payload");
455 this->set_error_(improv::ERROR_UNKNOWN_RPC);
456 this->incoming_data_.clear();
457 }
458 } else if (this->incoming_data_.size() - 2 > length) {
459 ESP_LOGV(TAG, "Too much data received or data malformed; resetting buffer");
460 this->incoming_data_.clear();
461 } else {
462 ESP_LOGV(TAG, "Waiting for split data packets");
463 }
464}
465
467 // Once BLE ended the session, Wi-Fi keeps trying these credentials, so they stay pending until it connects
468 if (this->state_ != improv::STATE_PROVISIONING)
469 return;
470 this->connecting_sta_ = {};
471 this->set_error_(improv::ERROR_UNABLE_TO_CONNECT);
472 this->set_state_(improv::STATE_AUTHORIZED);
473#ifdef USE_BINARY_SENSOR
474 if (this->authorizer_binary_sensor_ != nullptr)
475 this->authorized_start_ = millis();
476#endif
477 ESP_LOGW(TAG, "Timed out while connecting to Wi-Fi network");
479}
480
482 // Also waits for save_connecting_sta_(), which skips a pass while the joined SSID still reads empty
483 if (!wifi::global_wifi_component->is_connected() || !this->connecting_sta_.get_ssid().empty()) {
484 return;
485 }
486
487 if (this->state_ == improv::STATE_PROVISIONING) {
488 this->cancel_timeout(WIFI_CONNECT_TIMEOUT_ID);
489
490 // Build the URL list directly into a stack buffer with no heap allocation
491 std::array<uint8_t, improv::RPC_RESPONSE_MAX_SIZE> buf;
492 improv::RpcResponseBuilder builder(buf, improv::WIFI_SETTINGS);
493
494#ifdef USE_IMPROV_NEXT_URL
495 // Add next_url if configured (should be first per Improv BLE spec)
496 this->add_next_url_(builder, MAX_NEXT_URL_LEN);
497#endif
498
499 // Add default URLs for backward compatibility; MAX_NEXT_URL_LEN reserves this
500 // entry's space, so it always fits
501 builder.add_string(ESPHOME_MY_LINK, sizeof(ESPHOME_MY_LINK) - 1);
502#ifdef USE_WEBSERVER
503 for (auto &ip : wifi::global_wifi_component->wifi_sta_ip_addresses()) {
504 if (ip.is_ip4()) {
505 this->add_webserver_url_(builder, ip);
506 break;
507 }
508 }
509#endif
510 this->send_response_(builder.finish());
511 } else if (this->is_active() && this->state_ != improv::STATE_PROVISIONED) {
512 ESP_LOGD(TAG, "WiFi provisioned externally");
513 }
514
515 this->set_state_(improv::STATE_PROVISIONED);
516 this->stop();
517}
518
521 return;
522 std::array<char, wifi::SSID_BUFFER_SIZE> ssid;
523 const char *joined = wifi::global_wifi_component->wifi_ssid_to(ssid);
524 // Empty while a disconnect is still on its way; check again on the next pass
525 if (joined[0] == '\0')
526 return;
527 // Only for the network Improv asked for: the captive portal may have set another one since
528 if (this->connecting_sta_.get_ssid() == joined)
529 wifi::global_wifi_component->save_wifi_sta(this->connecting_sta_.get_ssid(), this->connecting_sta_.get_password());
530 this->connecting_sta_ = {};
531}
532
534 uint8_t service_data[IMPROV_SERVICE_DATA_SIZE] = {};
535 service_data[0] = IMPROV_PROTOCOL_ID_1; // PR
536 service_data[1] = IMPROV_PROTOCOL_ID_2; // IM
537 service_data[2] = static_cast<uint8_t>(this->state_);
538
539 uint8_t capabilities = 0x00;
540#ifdef USE_OUTPUT
541 if (this->status_indicator_ != nullptr)
542 capabilities |= improv::CAPABILITY_IDENTIFY;
543#endif
544
545 service_data[3] = capabilities;
546 // service_data[4-7] are already 0 (Reserved)
547
548 // Atomically set service data and disable name in advertising
549 esp32_ble::global_ble->advertising_set_service_data_and_name(std::span<const uint8_t>(service_data), false);
550}
551
554
555 // If we're advertising the device name and it's been more than NAME_ADVERTISING_DURATION, switch back to service data
556 if (this->advertising_device_name_) {
557 if (now - this->last_name_adv_time_ >= NAME_ADVERTISING_DURATION) {
558 ESP_LOGV(TAG, "Switching back to service data advertising");
559 this->advertising_device_name_ = false;
560 // Restore service data advertising
562 }
563 return;
564 }
565
566 // Check if it's time to advertise the device name (every NAME_ADVERTISING_INTERVAL)
567 if (now - this->last_name_adv_time_ >= NAME_ADVERTISING_INTERVAL) {
568 ESP_LOGV(TAG, "Switching to device name advertising");
569 this->advertising_device_name_ = true;
570 this->last_name_adv_time_ = now;
571
572 // Atomically clear service data and enable name in advertising data
573 esp32_ble::global_ble->advertising_set_service_data_and_name(std::span<const uint8_t>{}, true);
574 }
575}
576
578 if (this->advertising_requested_)
579 return;
580 this->advertising_requested_ = true;
581 // Passive scanners only see the advertising packet
584}
585
593
595#ifdef USE_BINARY_SENSOR
596 // If we have an authorizer, start in awaiting authorization state
597 return this->authorizer_binary_sensor_ == nullptr ? improv::STATE_AUTHORIZED : improv::STATE_AWAITING_AUTHORIZATION;
598#else
599 // No binary_sensor support = no authorizer possible, start as authorized
600 return improv::STATE_AUTHORIZED;
601#endif
602}
603
604ImprovBLEComponent *global_improv_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
605
606} // namespace esphome::improv_ble
607
608#endif
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.
bool cancel_timeout(const char *name)
Cancel a timeout function.
void enable_loop()
Enable this component's loop.
Definition component.h:262
void disable_loop()
Disable this component's loop.
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
constexpr bool empty() const
Definition string_ref.h:81
static constexpr ESPBTUUID from_raw(const uint8_t *data)
Construct from raw 16-byte little-endian UUID.
Definition ble_device.h:138
static ByteBuffer wrap(T value, Endian endianness=LITTLE)
Definition bytebuffer.h:155
void advertising_start()
Request advertising on behalf of a component.
Definition ble.cpp:196
void advertising_set_service_data_and_name(std::span< const uint8_t > data, bool include_name)
Definition ble.cpp:233
void advertising_clear_priority_uuid()
Definition ble.cpp:271
void advertising_set_priority_uuid(ESPBTUUID uuid)
Keep uuid and the service data alone in the advertising packet; applies on the next refresh.
Definition ble.cpp:266
void advertising_stop()
Release a request made with advertising_start(); advertising stops at the last release.
Definition ble.cpp:202
void on_write(std::function< void(std::span< const uint8_t >, uint16_t)> &&callback)
void add_descriptor(BLEDescriptor *descriptor)
ESPHOME_ALWAYS_INLINE bool is_running()
Definition ble_server.h:34
BLEService * create_service(ESPBTUUID uuid, bool advertise=false, uint16_t num_handles=15)
void remove_service(ESPBTUUID uuid, uint8_t inst_id=0)
void on_disconnect(std::function< void(uint16_t)> &&callback)
Definition ble_server.h:87
const ESPBTUUID & get_uuid() const
Definition ble_service.h:36
BLECharacteristic * create_characteristic(const std::string &uuid, esp_gatt_char_prop_t properties, esp_gatt_perm_t permissions=ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE)
void set_should_start(bool should_start)
Started by the server once created, and again after ble.enable.
Definition ble_service.h:59
void add_webserver_url_(improv::RpcResponseBuilder &builder, const network::IPAddress &ip)
Append "http://<ip>:<port>" to the response, warning if it does not fit.
void add_next_url_(improv::RpcResponseBuilder &builder, size_t max_len)
Append the formatted next_url to the RPC response, warning if it does not fit.
void set_state_(improv::State state, bool update_advertising=true)
const char * state_to_string_(improv::State state)
CallbackManager< void(improv::State, improv::Error)> state_callback_
void send_response_(std::span< const uint8_t > response)
virtual void set_state(bool state)
Enable or disable this binary output.
StringRef get_ssid() const
void set_ssid(const std::string &ssid)
void set_sta(const WiFiAP &ap)
void save_wifi_sta(const std::string &ssid, const std::string &password)
void start_connecting(const WiFiAP &ap)
const char * wifi_ssid_to(std::span< char, SSID_BUFFER_SIZE > buffer)
Write SSID to buffer without heap allocation.
bool state
Definition fan.h:2
ESP32BLE * global_ble
Definition ble.cpp:932
ESPHOME_LOG_TAG(TAG, "esp32_ble")
ImprovBLEComponent * global_improv_component
std::span< const uint8_t > data
ProvisioningManager * global_provisioning_manager
constexpr float AFTER_BLUETOOTH
Definition component.h:50
WiFiComponent * global_wifi_component
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t