@@ -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}
0 commit comments