@@ -81,6 +81,13 @@ static bool start_advertising(void) {
81
81
LOG (LL_ERROR , ("bt.dev_name or device.id must be set" ));
82
82
return false;
83
83
}
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
+ }
84
91
esp_bd_addr_t local_addr ;
85
92
uint8_t addr_type ;
86
93
esp_ble_gap_get_local_used_addr (local_addr , & addr_type );
@@ -91,13 +98,6 @@ static bool start_advertising(void) {
91
98
char addr [BT_ADDR_STR_LEN ];
92
99
LOG (LL_INFO , ("BT device name %s, addr %s" , dev_name ,
93
100
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
- }
101
101
return true;
102
102
}
103
103
@@ -420,6 +420,11 @@ static void esp32_gap_ev_handler(esp_gap_ble_cb_event_t ev,
420
420
}
421
421
}
422
422
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
+
423
428
bool esp32_bt_gap_init (void ) {
424
429
if (esp_ble_gap_register_callback (esp32_gap_ev_handler ) != ESP_OK ) {
425
430
return false;
@@ -460,5 +465,9 @@ bool esp32_bt_gap_init(void) {
460
465
s_adv_params .own_addr_type = BLE_ADDR_TYPE_PUBLIC ;
461
466
}
462
467
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;
464
473
}
0 commit comments