35#include "freertos/FreeRTOS.h"
36#include "freertos/timers.h"
37#include "freertos/xtensa_api.h"
38#include "freertos/FreeRTOSConfig.h"
39#include "freertos/queue.h"
40#include "freertos/task.h"
42#include "esp_bt_main.h"
43#include "esp_bt_device.h"
44#include "esp_gap_bt_api.h"
45#include "esp_a2dp_api.h"
46#include "driver/i2s.h"
47#include "esp_avrc_api.h"
48#include "esp_spp_api.h"
52#include "A2DPVolumeControl.h"
53#include "esp_task_wdt.h"
55#ifdef ARDUINO_ARCH_ESP32
56#include "esp32-hal-log.h"
57#include "esp32-hal-bt.h"
62extern "C" void delay(
long millis);
63extern "C" unsigned long millis();
68#if !defined(ESP_IDF_4) && !defined(I2S_COMM_FORMAT_STAND_I2S)
70# define I2S_COMM_FORMAT_STAND_I2S (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
71# define I2S_COMM_FORMAT_STAND_MSB (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB)
72# define I2S_COMM_FORMAT_STAND_PCM_LONG (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG)
73# define I2S_COMM_FORMAT_STAND_PCM_SHORT (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_SHORT)
91#define BT_AV_TAG "BT_AV"
92#define BT_RC_CT_TAG "RCCT"
93#define BT_APP_TAG "BT_API"
94#define APP_RC_CT_TL_GET_CAPS (0)
96enum ReconnectStatus { NoReconnect, AutoReconnect, IsReconnecting};
117 virtual bool connect_to(esp_bd_addr_t peer);
122 virtual void end(
bool releaseMemory=
false);
129 ESP_LOGI(BT_AV_TAG,
"set_volume: %d", volume);
130 volume_value = volume;
133 is_volume_used =
true;
138 return is_volume_used ? volume_value : 0;
143 volume_control_ptr = ptr;
164 virtual void debounce(
void(*cb)(
void),
int ms);
170 const char*
to_str(esp_a2d_connection_state_t state);
173 const char*
to_str(esp_a2d_audio_state_t state);
176 const char*
to_str(esp_bd_addr_t bda);
180 task_priority = priority;
191 event_queue_size = size;
196 event_stack_size = size;
201 return &last_connection;
206 virtual void set_discoverability(esp_bt_discovery_mode_t d);
210 esp_bd_addr_t peer_bd_addr;
211 ReconnectStatus reconnect_status = AutoReconnect;
212 unsigned long reconnect_timout=0;
213 unsigned int default_reconnect_timout=10000;
214 bool is_autoreconnect_allowed =
false;
215 uint32_t debounce_ms = 0;
218 esp_bd_addr_t last_connection = {0,0,0,0,0,0};
219 bool is_start_disabled =
false;
220 void (*connection_state_callback)(esp_a2d_connection_state_t state,
void* obj) =
nullptr;
221 void (*audio_state_callback)(esp_a2d_audio_state_t state,
void* obj) =
nullptr;
222 void (*audio_state_callback_post)(esp_a2d_audio_state_t state,
void* obj) =
nullptr;
223 void *connection_state_obj =
nullptr;
224 void *audio_state_obj =
nullptr;
225 void *audio_state_obj_post =
nullptr;
226 const char *m_a2d_conn_state_str[4] = {
"Disconnected",
"Connecting",
"Connected",
"Disconnecting"};
227 const char *m_a2d_audio_state_str[3] = {
"Suspended",
"Stopped",
"Started"};
228 esp_a2d_audio_state_t audio_state = ESP_A2D_AUDIO_STATE_STOPPED;
229 esp_a2d_connection_state_t connection_state = ESP_A2D_CONNECTION_STATE_DISCONNECTED;
230 UBaseType_t task_priority = configMAX_PRIORITIES - 10;
232 uint8_t volume_value = 0;
233 bool is_volume_used =
false;
234 BaseType_t task_core = 1;
236 int event_queue_size = 20;
237 int event_stack_size = 3072;
241 esp_bt_discovery_mode_t discoverability = ESP_BT_GENERAL_DISCOVERABLE;
244 virtual esp_err_t esp_a2d_connect(esp_bd_addr_t peer) = 0;
245 virtual const char* last_bda_nvs_name() = 0;
246 virtual void get_last_connection();
247 virtual void set_last_connection(esp_bd_addr_t bda);
248 virtual void clean_last_connection();
249 virtual bool has_last_connection();
251 virtual void set_scan_mode_connectable(
bool connectable);
255 return volume_control_ptr !=
nullptr ? volume_control_ptr : &default_volume_control;
void delay(long millis)
call vTaskDelay to deley for the indicated number of milliseconds
Definition: BluetoothA2DPCommon.cpp:342
bool btStart()
Startup logic as implemented by Arduino - This is not available if we use this library outside of Ard...
Definition: BluetoothA2DPCommon.cpp:316
void(* app_callback_t)(uint16_t event, void *param)
handler for the dispatched work
Definition: BluetoothA2DPCommon.h:80
Default implementation for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:88
Abstract class for handling of the volume of the audio data.
Definition: A2DPVolumeControl.h:28
Common Bluetooth A2DP functions.
Definition: BluetoothA2DPCommon.h:104
void set_event_stack_size(int size)
Defines the stack size of the event task (in bytes)
Definition: BluetoothA2DPCommon.h:195
virtual void set_connected(bool active)
Calls disconnect or reconnect.
Definition: BluetoothA2DPCommon.cpp:52
virtual void set_volume(uint8_t volume)
Sets the volume (range 0 - 255)
Definition: BluetoothA2DPCommon.h:128
virtual int get_volume()
Determines the actual volume.
Definition: BluetoothA2DPCommon.h:137
virtual bool is_connected()=0
Checks if A2DP is connected.
virtual void set_on_audio_state_changed_post(void(*callBack)(esp_a2d_audio_state_t state, void *), void *obj=nullptr)
Definition: BluetoothA2DPCommon.cpp:226
const char * to_str(esp_a2d_connection_state_t state)
converts esp_a2d_connection_state_t to a string
Definition: BluetoothA2DPCommon.cpp:247
virtual ~BluetoothA2DPCommon()=default
Destructor.
virtual esp_a2d_connection_state_t get_connection_state()
Determine the connection state.
Definition: BluetoothA2DPCommon.cpp:22
void set_auto_reconnect(bool active)
activate / deactivate the automatic reconnection to the last address (per default this is on)
Definition: BluetoothA2DPCommon.cpp:28
virtual void set_on_audio_state_changed(void(*callBack)(esp_a2d_audio_state_t state, void *), void *obj=nullptr)
Set the callback that is called when the audio state is changed.
Definition: BluetoothA2DPCommon.cpp:219
void set_task_priority(UBaseType_t priority)
defines the task priority (the default value is configMAX_PRIORITIES - 10)
Definition: BluetoothA2DPCommon.h:179
void set_task_core(BaseType_t core)
Defines the core which is used to start the tasks (to process the events and audio queue)
Definition: BluetoothA2DPCommon.h:185
virtual esp_a2d_audio_state_t get_audio_state()
Determine the actual audio state.
Definition: BluetoothA2DPCommon.cpp:18
virtual void end(bool releaseMemory=false)
Closes the connection and stops A2DP.
Definition: BluetoothA2DPCommon.cpp:76
virtual void set_volume_control(A2DPVolumeControl *ptr)
you can define a custom VolumeControl implementation
Definition: BluetoothA2DPCommon.h:142
void log_free_heap()
Logs the free heap.
Definition: BluetoothA2DPCommon.cpp:242
virtual A2DPVolumeControl * volume_control()
provides access to the VolumeControl object
Definition: BluetoothA2DPCommon.h:254
virtual void debounce(void(*cb)(void), int ms)
Prevents that the same method is executed multiple times within the indicated time limit.
Definition: BluetoothA2DPCommon.cpp:233
virtual void set_on_connection_state_changed(void(*callBack)(esp_a2d_connection_state_t state, void *), void *obj=nullptr)
Set the callback that is called when the connection state is changed.
Definition: BluetoothA2DPCommon.cpp:212
virtual void disconnect()
Closes the connection.
Definition: BluetoothA2DPCommon.cpp:62
virtual bool reconnect()
Reconnects to the last device.
Definition: BluetoothA2DPCommon.cpp:33
virtual esp_bd_addr_t * get_last_peer_address()
Provides the address of the last device.
Definition: BluetoothA2DPCommon.h:200
void set_event_queue_size(int size)
Defines the queue size of the event task.
Definition: BluetoothA2DPCommon.h:190
Internal message to be sent for BluetoothA2DPSink and BluetoothA2DPSource.
Definition: BluetoothA2DPCommon.h:83
app_callback_t cb
Definition: BluetoothA2DPCommon.h:86
uint16_t event
Definition: BluetoothA2DPCommon.h:85
uint16_t sig
Definition: BluetoothA2DPCommon.h:84
void * param
Definition: BluetoothA2DPCommon.h:87