Arduino A2DP
Loading...
Searching...
No Matches
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 set_ssp_enabled (bool active)
 activate Secure Simple Pairing
 
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
 
virtual void set_local_name (const char *name)
 Defines the local name.
 
virtual void start (const char *name, music_data_channels_cb_t callback=NULL)
 starts the bluetooth source
 
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
 
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
 
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 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.
 
virtual bool is_connected ()
 Returns true if the bluetooth device is connected.
 
virtual bool has_sound_data ()
 Returns true if write_dataRaw has been called with any valid data.
 
virtual void set_nvs_init (bool doInit)
 Defines if the Flash NVS should be reset on start.
 
virtual void set_reset_ble (bool doInit)
 Defines if the BLE should be reset on start.
 
virtual int32_t get_data_default (uint8_t *data, int32_t len)
 callback for data
 
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 disconnect ()
 Closes the connection.
 
virtual bool reconnect ()
 Reconnects to the last device.
 
virtual bool connect_to (esp_bd_addr_t peer)
 Connnects to the indicated address.
 
virtual void set_connected (bool active)
 Calls disconnect or reconnect.
 
virtual void end (bool releaseMemory=false)
 Closes the connection and stops A2DP.
 
virtual void set_volume (uint8_t volume)
 Sets the volume (range 0 - 255)
 
virtual int get_volume ()
 Determines the actual volume.
 
virtual void set_volume_control (A2DPVolumeControl *ptr)
 you can define a custom VolumeControl implementation
 
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 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.
 
virtual void set_on_audio_state_changed_post (void(*callBack)(esp_a2d_audio_state_t state, void *), void *obj=nullptr)
 
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.
 
virtual void debounce (void(*cb)(void), int ms)
 Prevents that the same method is executed multiple times within the indicated time limit.
 
void log_free_heap ()
 Logs the free heap.
 
const char * to_str (esp_a2d_connection_state_t state)
 converts esp_a2d_connection_state_t to a string
 
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_bd_addr_t bda)
 converts a esp_bd_addr_t to a string - the string is 18 characters long!
 
const char * to_str (esp_avrc_playback_stat_t state)
 converts esp_avrc_playback_stat_t to a string
 
void set_task_priority (UBaseType_t priority)
 defines the task priority (the default value is configMAX_PRIORITIES - 10)
 
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_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 esp_bd_addr_t * get_last_peer_address ()
 Provides the address of the last device.
 
virtual void set_discoverability (esp_bt_discovery_mode_t d)
 Bluetooth discoverability.
 
virtual void set_connectable (bool connectable)
 Bluetooth connectable.
 
virtual const char * get_name ()
 Provides the actual SSID name.
 
virtual void clean_last_connection ()
 clean last connection (delete)
 

Protected Member Functions

virtual void process_user_state_callbacks (uint16_t event, void *param)
 
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_task_start_up (void)
 
virtual void bt_app_task_shut_down (void)
 
virtual void bt_app_av_media_proc (uint16_t event, void *param)
 
virtual void bt_app_av_state_unconnected (uint16_t event, void *param)
 
virtual void bt_app_av_state_connecting (uint16_t event, void *param)
 
virtual void bt_app_av_state_connected (uint16_t event, void *param)
 
virtual void bt_app_av_state_disconnecting (uint16_t event, void *param)
 
virtual bool bt_app_send_msg (app_msg_t *msg)
 
virtual void bt_app_work_dispatched (app_msg_t *msg)
 
virtual bool get_name_from_eir (uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
 
virtual void filter_inquiry_scan_result (esp_bt_gap_cb_param_t *param)
 
virtual const char * last_bda_nvs_name ()
 
virtual void bt_av_hdl_stack_evt (uint16_t event, void *p_param)
 
virtual void bt_app_task_handler (void *arg)
 
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 void a2d_app_heart_beat (void *arg)
 
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_sm_hdlr (uint16_t event, void *param)
 A2DP application state machine.
 
virtual void bt_av_hdl_avrc_ct_evt (uint16_t event, void *p_param)
 avrc CT event handler
 
virtual void reset_last_connection ()
 resets the last connectioin so that we can reconnect
 
virtual esp_err_t esp_a2d_connect (esp_bd_addr_t peer)
 
void bt_av_notify_evt_handler (uint8_t event, esp_avrc_rn_param_t *param)
 
void bt_av_volume_changed (void)
 
virtual void get_last_connection ()
 
virtual void set_last_connection (esp_bd_addr_t bda)
 
virtual bool has_last_connection ()
 
virtual void set_scan_mode_connectable (bool connectable)
 Defines if the bluetooth is connectable.
 
virtual A2DPVolumeControlvolume_control ()
 provides access to the VolumeControl object
 

Protected Attributes

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

Friends

void ccall_bt_av_hdl_stack_evt (uint16_t event, void *p_param)
 
void ccall_bt_app_task_handler (void *arg)
 
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_a2d_app_heart_beat (void *arg)
 
void ccall_bt_app_a2d_cb (esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
 
void ccall_bt_app_av_sm_hdlr (uint16_t event, void *param)
 
void ccall_bt_av_hdl_avrc_ct_evt (uint16_t event, void *param)
 
int32_t ccall_bt_app_a2d_data_cb (uint8_t *data, int32_t len)
 
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_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.

◆ end()

void BluetoothA2DPCommon::end ( bool  releaseMemory = false)
virtualinherited

Closes the connection and stops A2DP.

Reimplemented in BluetoothA2DPSink.

◆ esp_a2d_connect()

virtual esp_err_t BluetoothA2DPSource::esp_a2d_connect ( esp_bd_addr_t  peer)
inlineprotectedvirtual

Implements BluetoothA2DPCommon.

◆ get_volume()

virtual int BluetoothA2DPCommon::get_volume ( )
inlinevirtualinherited

Determines the actual volume.

Reimplemented in BluetoothA2DPSink.

◆ is_connected()

bool BluetoothA2DPSource::is_connected ( )
virtual

Returns true if the bluetooth device is connected.

Implements BluetoothA2DPCommon.

◆ last_bda_nvs_name()

virtual const char * BluetoothA2DPSource::last_bda_nvs_name ( )
inlineprotectedvirtual

Implements BluetoothA2DPCommon.

◆ 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.

◆ set_volume()

virtual void BluetoothA2DPCommon::set_volume ( uint8_t  volume)
inlinevirtualinherited

Sets the volume (range 0 - 255)

Reimplemented in BluetoothA2DPSink.

◆ 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


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