Skip to content

Auto Reconnect

Phil Schatzmann edited this page Dec 14, 2021 · 25 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.

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

To open a Sink the code would look as follows:

#include "BluetoothA2DPSink.h"

BluetoothA2DPSink a2dp_sink;

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

void loop() {
}

The default value for AUTOCONNECT_TRY_NUM is 1000. 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.

When onNormal is set to true, the system also tries to reconnect after some normal disconnects. If you just want to reconnect on signal loss set the value to false!

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() {
}
Clone this wiki locally