Skip to content

Commit 12e71f7

Browse files
committed
bt_app: update initialization sequence
1 parent 06f8070 commit 12e71f7

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

main/src/user/bt_app.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,21 @@ static void bt_app_hdl_stack_evt(uint16_t event, void *p_param)
6666
esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb);
6767
esp_a2d_sink_init();
6868

69-
/* set discoverable and connectable mode, wait to be connected */
70-
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
69+
if (memcmp(last_remote_bda, "\x00\x00\x00\x00\x00\x00", 6) != 0) {
70+
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
71+
if (!(uxBits & OS_PWR_SLEEP_BIT) && !(uxBits & OS_PWR_RESET_BIT)) {
72+
ESP_LOGW(BT_APP_TAG, "connecting to [%02x:%02x:%02x:%02x:%02x:%02x]",
73+
last_remote_bda[0], last_remote_bda[1], last_remote_bda[2],
74+
last_remote_bda[3], last_remote_bda[4], last_remote_bda[5]);
75+
76+
esp_a2d_sink_connect(last_remote_bda);
77+
}
78+
} else {
79+
xEventGroupSetBits(user_event_group, BT_A2DP_IDLE_BIT);
80+
81+
/* set discoverable and connectable mode, wait to be connected */
82+
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
83+
}
7184

7285
break;
7386
}
@@ -82,8 +95,6 @@ void bt_app_init(void)
8295
size_t length = sizeof(esp_bd_addr_t);
8396
app_getenv("LAST_REMOTE_BDA", &last_remote_bda, &length);
8497

85-
xEventGroupSetBits(user_event_group, BT_A2DP_IDLE_BIT);
86-
8798
/* create application task */
8899
bt_app_task_start_up();
89100

@@ -103,17 +114,4 @@ void bt_app_init(void)
103114
esp_bt_gap_set_pin(pin_type, 4, pin_code);
104115

105116
ESP_LOGI(BT_APP_TAG, "started.");
106-
107-
if (memcmp(last_remote_bda, "\x00\x00\x00\x00\x00\x00", 6) != 0) {
108-
vTaskDelay(2000 / portTICK_RATE_MS);
109-
110-
EventBits_t uxBits = xEventGroupGetBits(user_event_group);
111-
if (uxBits & BT_A2DP_IDLE_BIT && !(uxBits & OS_PWR_SLEEP_BIT) && !(uxBits & OS_PWR_RESET_BIT)) {
112-
ESP_LOGW(BT_APP_TAG, "connecting to [%02x:%02x:%02x:%02x:%02x:%02x]",
113-
last_remote_bda[0], last_remote_bda[1], last_remote_bda[2],
114-
last_remote_bda[3], last_remote_bda[4], last_remote_bda[5]);
115-
116-
esp_a2d_sink_connect(last_remote_bda);
117-
}
118-
}
119117
}

main/src/user/bt_av.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,13 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
140140
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
141141
}
142142

143-
if (!(uxBits & BT_A2DP_IDLE_BIT)) {
144143
#ifdef CONFIG_ENABLE_AUDIO_PROMPT
145-
audio_player_play_file(MP3_FILE_IDX_DISCONNECTED);
144+
audio_player_play_file(MP3_FILE_IDX_DISCONNECTED);
146145
#endif
147146
#ifdef CONFIG_ENABLE_LED
148-
led_set_mode(LED_MODE_IDX_BLINK_M0);
147+
led_set_mode(LED_MODE_IDX_BLINK_M0);
149148
#endif
150-
xEventGroupSetBits(user_event_group, BT_A2DP_IDLE_BIT);
151-
}
149+
xEventGroupSetBits(user_event_group, BT_A2DP_IDLE_BIT);
152150
} else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
153151
xEventGroupClearBits(user_event_group, BT_A2DP_IDLE_BIT);
154152

0 commit comments

Comments
 (0)