Skip to content

Auto Reconnect

Phil Schatzmann edited this page May 16, 2024 · 24 revisions

It has been requested, that there should be an automatic reconnect when the connection has been lost. This functionality has been implemented both on the A2DP Source and the A2DP Sink.

While in older version, this setting was automatically active, it has been changed to be active only on request

A2DP Sink

I am not convinced that this is a good thing in all the cases, so I suggest that you explicitly specify it with the help of the set_auto_reconnect(bool active, int count=AUTOCONNECT_TRY_NUM); method.

To open a Sink - with auto reconnect active - the code would look as follows:

#include "BluetoothA2DPSink.h"

BluetoothA2DPSink a2dp_sink;

void setup() {
  a2dp_sink.set_auto_reconnect(true, 1000){
  a2dp_sink.start("MyMusic");  
}

void loop() {
}
  • The first parameter switches the functionality on or off. After the system tried unsuccessfully to reconnect, it will stop to retry and you need to start the connection from your Bluetooth Source (e.g. Mobile Phone) again. During this time it will be possible to open a connection from another Bluetooth source (e.g. phone).
  • The default value for AUTOCONNECT_TRY_NUM is 1000. This value might be ok if you usually connect with the same device but it is much too high if you want to work with multiple devices.

You can deactivate the automatic reconnect with:

#include "BluetoothA2DPSink.h"

BluetoothA2DPSink a2dp_sink;

void setup() {
  a2dp_sink.set_auto_reconnect(false);
  a2dp_sink.start("MyMusic");  
}

void loop() {
}

A2DP Source

Currently only the a2dp_source.set_auto_reconnect(bool) method is supported w/o any additional parameters.

Please note that when you just restart the ESP32, you might end up in the situation where you can't reconnect to the Bluetooth Speaker, because it did not notice that the connection got lost. To prevent this you need either properly end A2DP via the API or restart your speaker. This is issue is independent on the set_auto_reconnect setting selected above!

Clone this wiki locally