ESP32-A2DP: Cant connect to esp32 receiver from source after restarting receiver

` #include “BluetoothA2DPSink.h”

BluetoothA2DPSink a2dp_sink;

void read_data(const uint8_t *data, uint32_t len){ }

void setup() { Serial.begin(115200); a2dp_sink.set_stream_reader(read_data); }

void loop() { a2dp_sink.start(“ESP32_SPEAKER_TEST”,false); Serial.println(“started”); delay(60000); if(a2dp_sink.isConnected()) a2dp_sink.disconnect(); a2dp_sink.end(); Serial.println(“turned off”); delay(10000); } ` After starting the Bluetooth receiver I can’t connect to it from my phone. The library works perfectly if I’m not stopping and then starting the Bluetooth receiver but i need it for my project, any fixes?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 20 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Just to clarify, my use case is that i want the esp32 to only connect to a specific device, and to accept frequently disconnections/connections, but my esp32 was crashing by many connections, and the ESP.restart() along with disabling the clean_last_connection(), worked very well for me ❤️ @jokubasver you’re my hero 👍

@jokubasver Thank you so much for the sketch you provided, it worked like a charm for me using both the ESP.restart(); and the clean_last_connection();, along with the sketch logic you wrote, thank you so much!

I moved the setting to config.h and increased it to 10 In addition I added the new method set_try_reconnect_max_count()

If I modify void BluetoothA2DPCommon::clean_last_connection() in BluetoothA2DPCommon.cpp to:

void BluetoothA2DPCommon::clean_last_connection() {
    ESP_LOGD(BT_AV_TAG, "%s", __func__);
    //esp_bd_addr_t cleanBda = { 0 };
    //set_last_connection(cleanBda);
}

and in my sketch I add:

unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 10000; // Time after which we reboot if we are disconnected for 10 seconds

void loop() {
  //Get the current time since boot
  currentMillis = millis();

  // Check if we have connection
  if (a2dp_sink.get_connection_state() == ESP_A2D_CONNECTION_STATE_DISCONNECTED)
  {
    if (currentMillis - startMillis >= period)  //test whether the period has elapsed
    {
      startMillis = currentMillis;
      Serial.println("Connection state: disconnected - Rebooting ESP32 to try again");
      delay(50);
      ESP.restart();
    }
    return;
  }

Then the auto-reconnect issue when the phone is out of BT range, goes away. This gives a 10 second window to attempt a reconnection, and if it fails - the ESP reboots and tries again. The time window could probably be reduced, but 10 seconds works fine in my case.

Seems like not clearing the last connection together with a reboot let’s the phone reconnect at any given time.

Just doing that restart after 10 seconds on it’s own did not fix the issue, neither did the removal of clean_last_connection(); I had to use both in order to fix the issue (albeit it’s a hacky, dumb and bad solution, but hey… it works)

I thought that by removing clean_last_connection(); it wouldn’t let a different device to connect to the A2DP sink, but it still works fine (tested with two phones)

Unfortunately, clean_last_connection(); is a protected function, so it would be great if this function could be disabled in the sketch, without modding the library.