Skip to content

Add bt_music_sender_auto_reconnect.ino#776

Closed
00kenno wants to merge 1 commit intopschatzmann:mainfrom
00kenno:main
Closed

Add bt_music_sender_auto_reconnect.ino#776
00kenno wants to merge 1 commit intopschatzmann:mainfrom
00kenno:main

Conversation

@00kenno
Copy link
Copy Markdown

@00kenno 00kenno commented Mar 8, 2026

I wanted to enable auto reconnect.

Initially, I tried this:

void setup() {
  a2dp_source.set_auto_reconnect(true);
  a2dp_source.set_data_callback_in_frames(get_data_frames);
  a2dp_source.set_volume(30);
  a2dp_source.start("LEXON MINO L");
}

But it turns out set_auto_reconnect(true) should not be called before start().

Posting this here in case it helps someone else with the same problem.

@00kenno
Copy link
Copy Markdown
Author

00kenno commented Mar 9, 2026

In the start() function, the following code exists:

void BluetoothA2DPSource::start(std::vector<const char *> names) {
  ESP_LOGD(BT_APP_TAG, "%s, ", __func__);
  this->bt_names = names;
  is_end = false;
  is_autoreconnect_allowed = (reconnect_status == AutoReconnect);

  init_nvs();
  if (is_autoreconnect_allowed) {
    // get last connection if not available
    if (!has_last_connection()) {
      get_last_connection();
    }
  }
  :
}

When set_auto_reconnect(true) is called earlier in the setup() function during the first startup, the code inside this if block is executed. As a result, get_last_connection() is called even though no previous connection information exists yet.

This behavior may cause an issue on the initial run, since there is no stored connection to retrieve.

@pschatzmann
Copy link
Copy Markdown
Owner

I don't see any issue here: The only thing is, that the very first time you get some error messages because no last address exsists.

In this case it is just connecting to the indicated name.

@pschatzmann pschatzmann closed this Apr 9, 2026
@00kenno
Copy link
Copy Markdown
Author

00kenno commented Apr 9, 2026

Indeed, there seems to be nothing wrong with this source code. However, I couldn't get it to connect on my microcontroller no matter how many times I tried. What I'm looking for isn't an improvement to the source code. It's just a slightly overly detailed sample code. It reports that running set_auto_reconnect(true) before start() may result in a connection failure.

@00kenno
Copy link
Copy Markdown
Author

00kenno commented Apr 9, 2026

This problem occurs especially when connecting to multiple devices.

@pschatzmann
Copy link
Copy Markdown
Owner

I agree, if you are not carefull with the parameters you try to reconnect (for a long time) to the last device which might not be available!

@00kenno
Copy link
Copy Markdown
Author

00kenno commented Apr 11, 2026

I'm glad my intention was conveyed. I would really appreciate it if you could add a sample like the one I provided, or at least clearly state this in the comments of the sample.

@pschatzmann
Copy link
Copy Markdown
Owner

Here is my latest commit!

@00kenno
Copy link
Copy Markdown
Author

00kenno commented Apr 11, 2026

Thank you for making the changes!

@00kenno
Copy link
Copy Markdown
Author

00kenno commented Apr 12, 2026

I know this is a bit selfish, but I'd really appreciate it if you could also include this information in the A2DP Source documentation. That's where I encountered the problem.

@pschatzmann
Copy link
Copy Markdown
Owner

I extended the logic for the BluetoothA2DPSource set_auto_reconnect() to accept a retry count parameter to be consistent with the A2DP Sink implementation.

Here is the related commit.

@00kenno
Copy link
Copy Markdown
Author

00kenno commented Apr 12, 2026

Thank you so much for making changes to this great library based on my feedback!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants