Arduino A2DP
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
BluetoothA2DPSource Class Reference

A2DP Bluetooth Source. More...

#include <BluetoothA2DPSource.h>

Inheritance diagram for BluetoothA2DPSource:
BluetoothA2DPCommon

Public Member Functions

 BluetoothA2DPSource ()
 Constructor.
 
 ~BluetoothA2DPSource ()
 Destructor.
 
virtual void clean_last_connection ()
 clean last connection (delete)
 
virtual bool connect_to (esp_bd_addr_t peer)
 Connnects to the indicated address.
 
virtual void debounce (void(*cb)(void), int ms)
 Prevents that the same method is executed multiple times within the indicated time limit.
 
void delay_ms (uint32_t millis)
 calls vTaskDelay to pause for the indicated number of milliseconds
 
virtual void disconnect ()
 Closes the connection.
 
virtual void end (bool releaseMemory=false)
 Closes the connection and stops A2DP.
 
virtual esp_a2d_audio_state_t get_audio_state ()
 Determine the actual audio state.
 
virtual esp_a2d_connection_state_t get_connection_state ()
 Determine the connection state.
 
virtual int32_t get_data_default (uint8_t *data, int32_t len)
 callback for data
 
virtual esp_bd_addr_t * get_last_peer_address ()
 Provides the address of the last device.
 
unsigned long get_millis ()
 Provides the time in milliseconds since the last system boot.
 
virtual const char * get_name ()
 Provides the actual SSID name.
 
virtual int get_volume ()
 Determines the actual volume.
 
virtual bool has_sound_data ()
 Returns true if write_dataRaw has been called with any valid data.
 
virtual bool is_connected ()
 Checks if A2DP is connected.
 
virtual bool is_discovery_active ()
 Provides the current discovery state: returns true when the discovery is in progress.
 
void log_free_heap ()
 Logs the free heap.
 
virtual bool reconnect ()
 Reconnects to the last device.
 
virtual void set_auto_reconnect (bool active)
 activate / deactivate the automatic reconnection to the last address (per default this is on)
 
virtual void set_auto_reconnect (esp_bd_addr_t addr)
 automatically tries to reconnect to the indicated address
 
void set_bluedroid_config_t (esp_bluedroid_config_t cfg)
 Defines the esp_bluedroid_config_t: Available from IDF 5.2.1.
 
virtual void set_connectable (bool connectable)
 Bluetooth connectable.
 
virtual void set_connected (bool active)
 Calls disconnect or reconnect.
 
virtual void set_default_bt_mode (esp_bt_mode_t mode)
 Defines the default bt mode. The default is ESP_BT_MODE_CLASSIC_BT: use this e.g. to set to ESP_BT_MODE_BTDM.
 
virtual void set_discoverability (esp_bt_discovery_mode_t d)
 Bluetooth discoverability. More...
 
virtual void set_discovery_mode_callback (void(*callback)(esp_bt_gap_discovery_state_t discoveryMode))
 Define callback to be notified about bt discovery mode state changes.
 
void set_event_queue_size (int size)
 Defines the queue size of the event task.
 
void set_event_stack_size (int size)
 Defines the stack size of the event task (in bytes)
 
virtual void set_local_name (const char *name)
 Defines the local name.
 
virtual void set_nvs_init (bool doInit)
 Defines if the Flash NVS should be reset on start.
 
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. More...
 
virtual void set_on_audio_state_changed_post (void(*callBack)(esp_a2d_audio_state_t state, void *), void *obj=nullptr)
 
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. More...
 
virtual void set_pin_code (const char *pin_code, esp_bt_pin_type_t pin_type=ESP_BT_PIN_TYPE_VARIABLE)
 Defines the pin code. If nothing is defined we use "1234".
 
virtual void set_reset_ble (bool doInit)
 Defines if the BLE should be reset on start.
 
virtual void set_ssid_callback (bool(*callback)(const char *ssid, esp_bd_addr_t address, int rrsi))
 Define callback to be notified about the found ssids.
 
virtual void set_ssp_enabled (bool active)
 activate Secure Simple Pairing
 
void set_task_core (BaseType_t core)
 Defines the core which is used to start the tasks (to process the events and audio queue)
 
void set_task_priority (UBaseType_t priority)
 defines the task priority (the default value is configMAX_PRIORITIES - 10)
 
virtual void set_valid_cod_service (uint16_t filter)
 Defines the valid enumesp_bt_cod_srvc_t values that are used to identify an audio service. e.g (ESP_BT_COD_SRVC_RENDERING | ESP_BT_COD_SRVC_AUDIO | ESP_BT_COD_SRVC_TELEPHONY)
 
virtual void set_volume (uint8_t volume)
 Sets the volume (range 0 - 127)
 
virtual void set_volume_control (A2DPVolumeControl *ptr)
 you can define a custom VolumeControl implementation
 
virtual void start (const char *name, music_data_channels_cb_t callback=NULL)
 starts the bluetooth source More...
 
virtual void start (music_data_channels_cb_t callback=NULL)
 Starts w/o indicating the name. Connections will be managed via set_ssid_callback()
 
virtual void start (std::vector< const char * > names, music_data_channels_cb_t callback=NULL)
 starts the bluetooth source. Supports multiple alternative names
 
virtual void start_raw (const char *name, music_data_cb_t callback=NULL)
 starts the bluetooth source More...
 
virtual void start_raw (music_data_cb_t callback=NULL)
 Starts w/o indicating the name. Connections will be managed via set_ssid_callback()
 
virtual void start_raw (std::vector< const char * > names, music_data_cb_t callback=NULL)
 start_raw which supports multiple alternative names
 
const char * to_str (esp_a2d_audio_state_t state)
 converts a esp_a2d_audio_state_t to a string
 
const char * to_str (esp_a2d_connection_state_t state)
 converts esp_a2d_connection_state_t to a string
 
const char * to_str (esp_avrc_playback_stat_t state)
 converts esp_avrc_playback_stat_t to a string More...
 
const char * to_str (esp_bd_addr_t bda)
 converts a esp_bd_addr_t to a string - the string is 18 characters long!
 
virtual bool write_data (SoundData *data)
 write sound data: In some cases it is very difficult to use the callback function. As an alternative we provide this method where you can just send the data to a queue. It is your responsibility however that you handle the situation if the queue is full.
 

Protected Member Functions

virtual void a2d_app_heart_beat (void *arg)
 
virtual esp_err_t bluedroid_init ()
 
virtual void bt_app_a2d_cb (esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
 callback function for A2DP source
 
virtual void bt_app_av_media_proc (uint16_t event, void *param)
 
virtual void bt_app_av_sm_hdlr (uint16_t event, void *param)
 A2DP application state machine.
 
virtual void bt_app_av_state_connected_hdlr (uint16_t event, void *param)
 
virtual void bt_app_av_state_connecting_hdlr (uint16_t event, void *param)
 
virtual void bt_app_av_state_disconnecting_hdlr (uint16_t event, void *param)
 
virtual void bt_app_av_state_unconnected_hdlr (uint16_t event, void *param)
 
virtual void bt_app_gap_callback (esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
 
virtual void bt_app_rc_ct_cb (esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
 callback function for AVRCP controller
 
virtual bool bt_app_send_msg (bt_app_msg_t *msg)
 
virtual void bt_app_task_handler (void *arg)
 
virtual void bt_app_task_shut_down (void)
 
virtual void bt_app_task_start_up (void)
 
virtual bool bt_app_work_dispatch (bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback)
 
virtual void bt_app_work_dispatched (bt_app_msg_t *msg)
 
virtual void bt_av_hdl_avrc_ct_evt (uint16_t event, void *p_param)
 avrc CT event handler
 
virtual void bt_av_hdl_stack_evt (uint16_t event, void *p_param)
 
void bt_av_notify_evt_handler (uint8_t event, esp_avrc_rn_param_t *param)
 
void bt_av_volume_changed (void)
 
virtual bool bt_start ()
 Startup logic as implemented by Arduino. More...
 
virtual esp_err_t esp_a2d_connect (esp_bd_addr_t peer)
 
virtual void filter_inquiry_scan_result (esp_bt_gap_cb_param_t *param)
 
virtual void get_last_connection ()
 
virtual bool get_name_from_eir (uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
 
virtual bool has_last_connection ()
 
virtual bool is_valid_cod_service (uint32_t cod)
 returns true for ESP_BT_COD_SRVC_RENDERING,ESP_BT_COD_SRVC_AUDIO,ESP_BT_COD_SRVC_TELEPHONY
 
virtual const char * last_bda_nvs_name ()
 
virtual void process_user_state_callbacks (uint16_t event, void *param)
 
virtual void reset_last_connection ()
 resets the last connectioin so that we can reconnect
 
virtual void set_last_connection (esp_bd_addr_t bda)
 
virtual void set_scan_mode_connectable (bool connectable)
 Defines if the bluetooth is connectable.
 
void set_scan_mode_connectable_default () override
 
const char * to_state_str (int state)
 converts a APP_AV_STATE_ENUM to a string
 
virtual A2DPVolumeControlvolume_control ()
 provides access to the VolumeControl object
 

Protected Attributes

esp_a2d_audio_state_t audio_state = ESP_A2D_AUDIO_STATE_STOPPED
 
void(* audio_state_callback )(esp_a2d_audio_state_t state, void *obj) = nullptr
 
void(* audio_state_callback_post )(esp_a2d_audio_state_t state, void *obj) = nullptr
 
void * audio_state_obj = nullptr
 
void * audio_state_obj_post = nullptr
 
esp_bluedroid_config_t bluedroid_config
 
esp_bt_mode_t bt_mode = ESP_BT_MODE_CLASSIC_BT
 
const char * bt_name = {0}
 
std::vector< const char * > bt_names
 
esp_a2d_connection_state_t connection_state = ESP_A2D_CONNECTION_STATE_DISCONNECTED
 
void(* connection_state_callback )(esp_a2d_connection_state_t state, void *obj) = nullptr
 
void * connection_state_obj = nullptr
 
music_data_cb_t data_stream_callback
 
music_data_channels_cb_t data_stream_channels_callback
 
uint32_t debounce_ms = 0
 
unsigned int default_reconnect_timout =10000
 
A2DPDefaultVolumeControl default_volume_control
 
const char * dev_name = "ESP32_A2DP_SRC"
 
esp_bt_discovery_mode_t discoverability = ESP_BT_GENERAL_DISCOVERABLE
 
bool discovery_active = false
 
void(* discovery_mode_callback )(esp_bt_gap_discovery_state_t discoveryMode) = nullptr
 
int event_queue_size = 20
 
int event_stack_size = 3072
 
bool has_sound_data_flag = false
 
bool is_autoreconnect_allowed = false
 
bool is_connecting = false
 
bool is_start_disabled = false
 
bool is_volume_used = false
 
esp_bd_addr_t last_connection = {0,0,0,0,0,0}
 
const char * m_a2d_audio_state_str [3] = {"Suspended", "Stopped", "Started"}
 
const char * m_a2d_conn_state_str [4] = {"Disconnected", "Connecting", "Connected", "Disconnecting"}
 
const char * m_avrc_playback_state_str [5] = {"stopped", "playing", "paused", "forward seek", "reverse seek"}
 
bool nvs_init = true
 
esp_bd_addr_t peer_bd_addr
 
esp_bt_pin_code_t pin_code
 
uint32_t pin_code_len
 
esp_bt_pin_type_t pin_type
 
ReconnectStatus reconnect_status = NoReconnect
 
unsigned long reconnect_timout =0
 
bool reset_ble = false
 
APP_AV_STATE s_a2d_last_state = APP_AV_STATE_IDLE
 
APP_AV_STATE s_a2d_state = APP_AV_STATE_IDLE
 
esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap
 
TaskHandle_t s_bt_app_task_handle = nullptr
 
QueueHandle_t s_bt_app_task_queue = nullptr
 
int s_connecting_heatbeat_count
 
int s_intv_cnt =0
 
int s_media_state =0
 
uint8_t s_peer_bdname [ESP_BT_GAP_MAX_BDNAME_LEN+1]
 
uint32_t s_pkt_cnt
 
TimerHandle_t s_tmr
 
SoundDatasound_data = nullptr
 
int32_t sound_data_current_pos = 0
 
bool(* ssid_callback )(const char *ssid, esp_bd_addr_t address, int rrsi) = nullptr
 
bool ssp_enabled =false
 
BaseType_t task_core = 1
 
UBaseType_t task_priority = configMAX_PRIORITIES - 10
 
uint16_t valid_cod_services = ESP_BT_COD_SRVC_RENDERING | ESP_BT_COD_SRVC_AUDIO | ESP_BT_COD_SRVC_TELEPHONY
 
A2DPVolumeControlvolume_control_ptr = nullptr
 
uint8_t volume_value = 0
 

Friends

void ccall_a2d_app_heart_beat (TIMER_ARG_TYPE arg)
 
void ccall_bt_app_a2d_cb (esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
 
int32_t ccall_bt_app_a2d_data_cb (uint8_t *data, int32_t len)
 
void ccall_bt_app_av_sm_hdlr (uint16_t event, void *param)
 
void ccall_bt_app_gap_callback (esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
 
void ccall_bt_app_rc_ct_cb (esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
 
void ccall_bt_app_task_handler (void *arg)
 
void ccall_bt_av_hdl_avrc_ct_evt (uint16_t event, void *param)
 
void ccall_bt_av_hdl_stack_evt (uint16_t event, void *p_param)
 
int32_t ccall_get_channel_data_wrapper (uint8_t *data, int32_t len)
 
int32_t ccall_get_data_default (uint8_t *data, int32_t len)
 

Detailed Description

A2DP Bluetooth Source.

Author
Phil Schatzmann

Member Function Documentation

◆ bt_app_av_state_connecting_hdlr()

void BluetoothA2DPSource::bt_app_av_state_connecting_hdlr ( uint16_t  event,
void *  param 
)
protectedvirtual

Switch state to APP_AV_STATE_UNCONNECTED when connecting lasts more than 2 heart beat intervals.

◆ bt_av_hdl_stack_evt()

void BluetoothA2DPSource::bt_av_hdl_stack_evt ( uint16_t  event,
void *  p_param 
)
protectedvirtual

The following mthods are called by the framework. They are public so that they can be executed from a extern "C" function.

◆ bt_start()

bool BluetoothA2DPCommon::bt_start ( )
protectedvirtualinherited

Startup logic as implemented by Arduino.

Returns
true
false

◆ set_discoverability()

void BluetoothA2DPCommon::set_discoverability ( esp_bt_discovery_mode_t  d)
virtualinherited

Bluetooth discoverability.

Defines if the bluetooth is discoverable.

◆ set_on_audio_state_changed()

void BluetoothA2DPCommon::set_on_audio_state_changed ( void(*)(esp_a2d_audio_state_t state, void *)  callBack,
void *  obj = nullptr 
)
virtualinherited

Set the callback that is called when the audio state is changed.

Set the callback that is called when the audio state is changed This callback is called before the I2S bus is changed.

◆ set_on_audio_state_changed_post()

void BluetoothA2DPCommon::set_on_audio_state_changed_post ( void(*)(esp_a2d_audio_state_t state, void *)  callBack,
void *  obj = nullptr 
)
virtualinherited

Set the callback that is called after the audio state has changed. This callback is called after the I2S bus has changed.

◆ set_on_connection_state_changed()

void BluetoothA2DPCommon::set_on_connection_state_changed ( void(*)(esp_a2d_connection_state_t state, void *)  callBack,
void *  obj = nullptr 
)
virtualinherited

Set the callback that is called when the connection state is changed.

Set the callback that is called when the connection state is changed This callback is called before the I2S bus is changed.

◆ start()

void BluetoothA2DPSource::start ( const char *  name,
music_data_channels_cb_t  callback = NULL 
)
virtual

starts the bluetooth source

Parameters
nameBluetooth name of the device to connect to
callbackfunction that provides the audio stream as array of Frame

◆ start_raw()

void BluetoothA2DPSource::start_raw ( const char *  name,
music_data_cb_t  callback = NULL 
)
virtual

starts the bluetooth source

Parameters
nameBluetooth name of the device to connect to
callbackfunction that provides the audio stream - The supported audio codec in ESP32 A2DP is SBC. SBC audio stream is encoded from PCM data normally formatted as 44.1kHz sampling rate, two-channel 16-bit sample data
is_ssp_enabledFlag to activate Secure Simple Pairing

◆ to_str()

const char * BluetoothA2DPCommon::to_str ( esp_avrc_playback_stat_t  state)
inherited

converts esp_avrc_playback_stat_t to a string

converts a esp_a2d_audio_state_t to a string

Member Data Documentation

◆ bluedroid_config

esp_bluedroid_config_t BluetoothA2DPCommon::bluedroid_config
protectedinherited
Initial value:
{
.ssp_en = true
}

The documentation for this class was generated from the following files: