1+ #include < A2DPVolumeControl.h>
12I2SStream i2s;
23BluetoothA2DPSink a2dp_sink (i2s);
4+ A2DPNoVolumeControl noVolumeControl;
35
46volatile bool md_album_recvd=0 , md_artist_recvd=0 , md_title_recvd=0 ;
57
@@ -60,16 +62,15 @@ void a2dp_init(){
6062 a2dp_sink.set_avrc_metadata_attribute_mask (ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM);
6163 a2dp_sink.set_on_connection_state_changed (a2dp_connection_state_changed);
6264 a2dp_sink.set_on_audio_state_changed (a2dp_audio_state_changed);
63-
64- #ifndef DEBUG
65- a2dp_sink.set_auto_reconnect (true );
66- #endif
65+ a2dp_sink.set_volume_control (&noVolumeControl);
66+ a2dp_sink.set_reconnect_delay (500 );
67+ a2dp_sink.set_auto_reconnect (true , 2000 );
6768
6869 a2dp_sink.start (" EHU32" ); // setting up bluetooth audio sink
6970 a2dp_started=1 ;
7071 DEBUG_PRINTLN (" A2DP: Started!" );
7172 disp_mode=0 ; // set display mode to audio metadata on boot
72- writeTextToDisplay (1 , " EHU32 v0.9rc started!" , " Bluetooth on" , " Waiting for connection..." );
73+ writeTextToDisplay (1 , " EHU32 v0.9.1 started!" , " Bluetooth on" , " Waiting for connection..." );
7374}
7475
7576// handles events such as connecion/disconnection and audio play/pause
@@ -91,6 +92,7 @@ void A2DP_EventHandler(){
9192
9293 if (bt_state_changed){ // mute external DAC when not playing
9394 if (bt_connected){
95+ a2dp_sink.set_volume (127 ); // workaround to ensure max volume being applied on successful connection
9496 writeTextToDisplay (1 , " Bluetooth connected" , " " , (char *)a2dp_sink.get_peer_name ());
9597 } else {
9698 writeTextToDisplay (1 , " Bluetooth disconnected" , " " , " " );
@@ -101,16 +103,11 @@ void A2DP_EventHandler(){
101103
102104// ID 0x501 DB3 0x18 indicates imminent shutdown of the radio and display; disconnect from source
103105void a2dp_shutdown (){
106+ ESP.restart (); // very crude workaround until I find a better way to deal with reconnection problems after end() is called
107+ delay (1000 );
104108 a2dp_sink.disconnect ();
105109 a2dp_sink.end ();
106110 ehu_started=0 ; // so it is possible to restart and reconnect the source afterwards in the rare case radio is shutdown but ESP32 is still powered up
107111 a2dp_started=0 ; // while extremely unlikely to happen in the vehicle, this comes handy for debugging on my desk setup
108112 DEBUG_PRINTLN (" CAN: EHU went down! Disconnecting A2DP." );
109- }
110-
111- void a2dp_end (){
112- a2dp_sink.disconnect ();
113- a2dp_sink.end ();
114- a2dp_started=0 ;
115- DEBUG_PRINTLN (" A2DP: Stopped!" );
116113}
0 commit comments