Skip to content

Device Discovery

pschatzmann edited this page Oct 3, 2026 · 1 revision

When you start the A2DP Source, it searches for Bluetooth devices that provide an audio service and connects to the device you have selected.

Connecting by Name

a2dp_source.start("MyMusic");

You can also indicate multiple alternative names. The source connects to the first one that is available:

static std::vector<const char*> bt_names = {"MyMusic", "RadioPlayer", "MusicPlayer"};
a2dp_source.start(bt_names);

A device matches if its name starts with the indicated name.

Selecting the Device with a Callback

If you call start() without any names, you can decide yourself with the ssid callback: it is called for each device that has been found. Return true to connect, or false to continue scanning. You can also use this to build a list of the available devices.

bool isValid(const char* ssid, esp_bd_addr_t address, int rssi) {
  Serial.print("available SSID: ");
  Serial.println(ssid);
  return strcmp(ssid, "MyMusic") == 0;
}

void setup() {
  Serial.begin(115200);
  a2dp_source.set_ssid_callback(isValid);
  a2dp_source.set_data_callback_in_frames(get_data_frames);
  a2dp_source.start();
}

See the bt_music_sender_ssid_callback example.

Filtering by Service

Only devices whose class of device indicates an audio service are considered. By default these are ESP_BT_COD_SRVC_RENDERING | ESP_BT_COD_SRVC_AUDIO | ESP_BT_COD_SRVC_TELEPHONY. You can change this filter with set_valid_cod_service().

Discovery State

  • set_discovery_mode_callback(): is called when the discovery starts or stops
  • is_discovery_active(): returns true while the discovery is running
  • cancel_discovery(): stops the discovery

Related Settings

  • set_local_name(): the name of the ESP32 that is shown on the sink
  • set_pin_code() and set_ssp_enabled(): pairing, see Access Protection
  • Reconnecting to the last device: see Auto Reconnect

Clone this wiki locally