Skip to content

Commit febfe6d

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
Delay enabling BT advertisement until after init to let BT start up
CL: Delay enabling BT advertisement until after init to let BT start up PUBLISHED_FROM=3338d95e65dddc30ca5422d0605a536916c8dc16
1 parent a2e31c9 commit febfe6d

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/esp32/esp32_bt_gap.c

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ static bool start_advertising(void) {
8181
LOG(LL_ERROR, ("bt.dev_name or device.id must be set"));
8282
return false;
8383
}
84+
if (esp_ble_gap_set_device_name(dev_name) != ESP_OK) {
85+
return false;
86+
}
87+
if (esp_ble_gap_config_adv_data(&s_adv_data)) {
88+
LOG(LL_ERROR, ("Failed to set adv data"));
89+
return false;
90+
}
8491
esp_bd_addr_t local_addr;
8592
uint8_t addr_type;
8693
esp_ble_gap_get_local_used_addr(local_addr, &addr_type);
@@ -91,13 +98,6 @@ static bool start_advertising(void) {
9198
char addr[BT_ADDR_STR_LEN];
9299
LOG(LL_INFO, ("BT device name %s, addr %s", dev_name,
93100
mgos_bt_addr_to_str(&la, MGOS_BT_ADDR_STRINGIFY_TYPE, addr)));
94-
if (esp_ble_gap_set_device_name(dev_name) != ESP_OK) {
95-
return false;
96-
}
97-
if (esp_ble_gap_config_adv_data(&s_adv_data)) {
98-
LOG(LL_ERROR, ("Failed to set adv data"));
99-
return false;
100-
}
101101
return true;
102102
}
103103

@@ -420,6 +420,11 @@ static void esp32_gap_ev_handler(esp_gap_ble_cb_event_t ev,
420420
}
421421
}
422422

423+
static void adv_enable_cb(void *arg) {
424+
mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable());
425+
(void) arg;
426+
}
427+
423428
bool esp32_bt_gap_init(void) {
424429
if (esp_ble_gap_register_callback(esp32_gap_ev_handler) != ESP_OK) {
425430
return false;
@@ -460,5 +465,9 @@ bool esp32_bt_gap_init(void) {
460465
s_adv_params.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
461466
}
462467

463-
return mgos_bt_gap_set_adv_enable(mgos_sys_config_get_bt_adv_enable());
468+
/* Delay until later, we've only just started the BT system and
469+
* sometimes this throws a "No random address yet" error. */
470+
mgos_invoke_cb(adv_enable_cb, NULL, false /* from_isr */);
471+
472+
return true;
464473
}

0 commit comments

Comments
 (0)