arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
BLESerialClientESP32 Class Reference

A BLE client that implements a serial protocol, so that it can be used to send and receive audio. In BLE terminology this is a Central. This implementation uses the BLE library of the ESP32 Arduino core (Bluedroid or NimBLE). More...

#include <BLESerialClientESP32.h>

Inheritance diagram for BLESerialClientESP32:
Inheritance graph
[legend]
Collaboration diagram for BLESerialClientESP32:
Collaboration graph
[legend]

Public Member Functions

 BLESerialClientESP32 (int mtu=517)
 
virtual void addNotifyAudioChange (AudioInfoSupport &bi)
 Adds target to be notified about audio changes.
 
virtual AudioInfo audioInfo () override
 provides the actual input AudioInfo
 
virtual AudioInfo audioInfoOut ()
 
int available () override
 
int availableForWrite () override
 
virtual bool begin ()
 
bool begin (const char *localName, int seconds)
 
virtual void clearNotifyAudioChange ()
 Deletes all change notify subscriptions.
 
bool connected () override
 Returns true if we are connected with the other side.
 
void end () override
 
void flush () override
 Sends the buffered data which does not fill a complete packet.
 
bool isNotifyActive ()
 Checks if the automatic AudioInfo update is active.
 
 operator bool () override
 
int packetSize ()
 
size_t readBytes (uint8_t *data, size_t len) override
 
virtual size_t readSilence (uint8_t *buffer, size_t length)
 Source to generate silence: just sets the buffer to 0.
 
virtual bool removeNotifyAudioChange (AudioInfoSupport &bi)
 Removes a target in order not to be notified about audio changes.
 
void setAudioInfo (AudioInfo newInfo) override
 
void setAudioInfoActive (bool flag)
 Activates the exchange of the audio info.
 
void setAudioInfoUUID (const char *uuid)
 Defines the UUID of the audio info characteristic.
 
void setConfirmWrite (bool flag)
 Writes with response: slower but with flow control.
 
void setFramed (bool flag)
 Keeps the write sizes: each write is one BLE packet.
 
void setMTU (int mtu)
 Defines the requested MTU (must be called before begin)
 
void setNotifyActive (bool flag)
 Deactivate/Reactivate automatic AudioInfo updates: (default is active)
 
void setRxBufferSize (int bytes, int packets=100)
 
void setRxUUID (const char *uuid)
 Defines the UUID of the characteristic which is written by the client.
 
void setServiceUUID (const char *uuid)
 Defines the service UUID.
 
void setTxBufferSize (int bytes, int packets=100)
 
void setTxUUID (const char *uuid)
 Defines the UUID of the characteristic which is read by the client.
 
void setWriteBufferSize (int size)
 
void setWriteThrottle (int ms)
 Waits the indicated ms after each write.
 
AudioInfo toInfo (const uint8_t *data, size_t len)
 Converts the string from the info characteristic to the audio info.
 
StrView toStr (AudioInfo info)
 
size_t write (const uint8_t *data, size_t len) override
 
virtual size_t write (uint8_t ch) override
 
virtual void writeSilence (size_t len)
 Writes len bytes of silence (=0).
 

Protected Member Functions

bool connect () override
 Connects to the server if it was found: returns true if successful.
 
int getMTU () override
 Effective MTU.
 
bool isConnected () override
 Returns true if we are connected.
 
virtual int not_supported (int out, const char *msg="")
 
void notifyAudioChange (AudioInfo info)
 
void onConnect (BLEClient *pClient) override
 
void onConnected ()
 Common setup after the connection was established.
 
void onDisconnect (BLEClient *pClient) override
 
void onResult (BLEAdvertisedDevice advertisedDevice) override
 scanning results
 
bool readAudioInfo () override
 Requests the audio info from the server: returns true if successful.
 
int readPacket (uint8_t *data, int maxLen) override
 Reads the next packet (incl. header) from the server: returns the size.
 
void receiveAudioInfo (const uint8_t *data, size_t size)
 Process the audio info which was received from the other side.
 
void receivePacket ()
 Reads the next packet from the server into the receive buffer.
 
bool reconnectLater ()
 disconnect and scan again
 
void refillReadBuffer ()
 Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()
 
bool sendPacket (const uint8_t *data, size_t len)
 
bool startClient (const char *localName) override
 Starts BLE and the scanning for the server.
 
void startScan ()
 
void writeAudioInfo (StrView str) override
 Provides the audio info to the other side.
 
bool writePacket (const uint8_t *data, int len) override
 Writes a packet to the server.
 

Static Protected Member Functions

static int getHeader (const uint8_t *packet, int packetLen)
 Determines the payload length from the 2 byte header.
 
static void notifyCallback (BLERemoteCharacteristic *characteristic, uint8_t *data, size_t length, bool isNotify)
 
static BLESerialClientESP32 *& selfBLESerialClientESP32 ()
 
static void setHeader (uint8_t *packet, int len)
 Copies the payload length into the 2 byte header.
 

Protected Attributes

int _timeout = 10
 
BLEAdvertisedDevice advertised_device
 
char audio_info_str [40]
 
const char * BLE_CH1_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
 
const char * BLE_CH2_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
 
const char * BLE_INFO_UUID = "6e400004-b5a3-f393-e0a9-e50e24dcca9e"
 
const char * BLE_SERIAL_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
 
BLERemoteCharacteristic * ch01_char = nullptr
 
BLERemoteCharacteristic * ch02_char = nullptr
 
AudioInfo info
 
BLERemoteCharacteristic * info_char = nullptr
 
bool is_audio_info_active = false
 
bool is_audio_info_defined = false
 
volatile bool is_found = false
 
bool is_framed = false
 
bool is_notify_active = true
 
volatile bool is_setup = false
 
bool is_started = false
 
int max_transfer_size = 0
 
Vector< AudioInfoSupport * > notify_vector
 
BLEClient * p_client = nullptr
 
Vector< uint8_t > read_packet {0}
 
BLESerialPacketBuffer rx_buffer
 
int rx_buffer_size = 4096
 
Mutex rx_mutex
 
int rx_packet_count = 100
 
RingBuffer< uint8_t > tmp_in {0}
 
RingBuffer< uint8_t > tmp_out {0}
 
int tx_buffer_size = 4096
 
int tx_packet_count = 100
 
SingleBuffer< uint8_t > write_buffer {0}
 
int write_buffer_size = MAX_SINGLE_CHARS
 
bool write_confirmation_flag = false
 
int write_throttle = 0
 

Detailed Description

A BLE client that implements a serial protocol, so that it can be used to send and receive audio. In BLE terminology this is a Central. This implementation uses the BLE library of the ESP32 Arduino core (Bluedroid or NimBLE).

Author
Phil Schatzmann

Constructor & Destructor Documentation

◆ BLESerialClientESP32()

BLESerialClientESP32 ( int  mtu = 517)
inline

Member Function Documentation

◆ addNotifyAudioChange()

virtual void addNotifyAudioChange ( AudioInfoSupport &  bi)
inlinevirtualinherited

◆ audioInfo()

virtual AudioInfo audioInfo ( )
inlineoverridevirtualinherited

◆ audioInfoOut()

virtual AudioInfo audioInfoOut ( )
inlinevirtualinherited

◆ available()

int available ( )
inlineoverridevirtualinherited

Reimplemented from BaseStream.

◆ availableForWrite()

int availableForWrite ( )
inlineoverridevirtualinherited

Reimplemented from BaseStream.

◆ begin() [1/2]

virtual bool begin ( )
inlinevirtualinherited

Reimplemented in AudioOutputWithCallback, AudioMP34DT05, I2SCodecStream, MozziStream, PureDataStream, SPDIFOutput, VS1053Stream, WM8960Stream, AdaptiveResamplingStream, AudioLoRa, ESPNowStream, USBAudioDeviceBase, USBAudioHostBase, VBANStream, FrequencyDetectorAutoCorrelation, GoertzelStream, AudioEffectStreamT< T >, AudioEffectStream, Equalizer3BandsT< T >, Equalizer3BandsPerChannelT< T >, I2SStream, AudioTimeSourceStream, AudioStreamWrapper, TimerCallbackAudioStream, ResampleStreamT< TInterpolator >, FileLoopT< FileType >, FileLoopT< File >, EncodedAudioStream, AudioBoardStream, AudioKitStream, MiniAudioStream, PortAudioStream, StdioStream, HLSStreamT< URLStream >, NRF24Stream, ReedSolomonFEC< bytecount, additional_bytes >, AutomaticGainControlStream, MeasuringStream, QualityAnalysisStream, VolumeStream, AnalogAudioStream, AdapterAudioOutputToAudioStream, TimedStream, MemoryStream, GeneratedSoundStream< T >, GeneratedSoundStream< int16_t >, ProgressStream, Throttle, InputMerge< T >, CallbackStream, FilteredStream< T, TF >, FilteredStream< int16_t, int16_t >, VolumeMeter, AudioInputMonitor, ChannelFormatConverterStreamT< T >, ChannelFormatConverterStream, NumberFormatConverterStreamT< TFrom, TTo >, NumberFormatConverterStream, FormatConverterStream, CatStream, QueueStream< T >, QueueStream< uint8_t >, DynamicMemoryStream, Pipeline, Pipeline::ModifyingStreamAdapter, ResampleStream, SupportedRatesStream, AudioFFTBase, and EqualizerNBands< SampleT, AccT, NUM_TAPS, NUM_BANDS >.

◆ begin() [2/2]

bool begin ( const char *  localName,
int  seconds 
)
inlineinherited

Starts the BLE client and waits at most the indicated seconds for the connection to the server. With 0 seconds we connect in the background.

◆ clearNotifyAudioChange()

virtual void clearNotifyAudioChange ( )
inlinevirtualinherited

Deletes all change notify subscriptions.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ connect()

bool connect ( )
inlineoverrideprotectedvirtual

Connects to the server if it was found: returns true if successful.

Implements BLESerialClientBase.

◆ connected()

bool connected ( )
inlineoverridevirtualinherited

Returns true if we are connected with the other side.

Implements BLESerialStream.

◆ end()

void end ( )
inlineoverridevirtual

Implements BLESerialStream.

◆ flush()

void flush ( )
inlineoverridevirtualinherited

Sends the buffered data which does not fill a complete packet.

Reimplemented from BaseStream.

◆ getHeader()

static int getHeader ( const uint8_t *  packet,
int  packetLen 
)
inlinestaticprotectedinherited

Determines the payload length from the 2 byte header.

◆ getMTU()

int getMTU ( )
inlineoverrideprotectedvirtual

Effective MTU.

Reimplemented from BLESerialStream.

◆ isConnected()

bool isConnected ( )
inlineoverrideprotectedvirtual

Returns true if we are connected.

Implements BLESerialClientBase.

◆ isNotifyActive()

bool isNotifyActive ( )
inlineinherited

Checks if the automatic AudioInfo update is active.

◆ not_supported()

virtual int not_supported ( int  out,
const char *  msg = "" 
)
inlineprotectedvirtualinherited

◆ notifyAudioChange()

void notifyAudioChange ( AudioInfo  info)
inlineprotectedinherited

◆ notifyCallback()

static void notifyCallback ( BLERemoteCharacteristic *  characteristic,
uint8_t *  data,
size_t  length,
bool  isNotify 
)
inlinestaticprotected

◆ onConnect()

void onConnect ( BLEClient *  pClient)
inlineoverrideprotected

◆ onConnected()

void onConnected ( )
inlineprotectedinherited

Common setup after the connection was established.

◆ onDisconnect()

void onDisconnect ( BLEClient *  pClient)
inlineoverrideprotected

◆ onResult()

void onResult ( BLEAdvertisedDevice  advertisedDevice)
inlineoverrideprotected

scanning results

◆ operator bool()

operator bool ( )
inlineoverridevirtualinherited

Reimplemented from AudioStream.

◆ packetSize()

int packetSize ( )
inlineinherited

Max number of payload bytes in one BLE packet: the packets read by the client also contain the length header and a value can not be longer than 512 bytes

◆ readAudioInfo()

bool readAudioInfo ( )
inlineoverrideprotectedvirtual

Requests the audio info from the server: returns true if successful.

Implements BLESerialClientBase.

◆ readBytes()

size_t readBytes ( uint8_t *  data,
size_t  len 
)
inlineoverridevirtualinherited

Reimplemented from AudioStream.

◆ readPacket()

int readPacket ( uint8_t *  data,
int  maxLen 
)
inlineoverrideprotectedvirtual

Reads the next packet (incl. header) from the server: returns the size.

Implements BLESerialClientBase.

◆ readSilence()

virtual size_t readSilence ( uint8_t *  buffer,
size_t  length 
)
inlinevirtualinherited

Source to generate silence: just sets the buffer to 0.

◆ receiveAudioInfo()

void receiveAudioInfo ( const uint8_t *  data,
size_t  size 
)
inlineprotectedinherited

Process the audio info which was received from the other side.

◆ receivePacket()

void receivePacket ( )
inlineprotectedinherited

Reads the next packet from the server into the receive buffer.

◆ reconnectLater()

bool reconnectLater ( )
inlineprotected

disconnect and scan again

◆ refillReadBuffer()

void refillReadBuffer ( )
inlineprotectedinherited

Refill small read buffer (e.g. 8 bytes) to avoid single byte reads when calling read()

◆ removeNotifyAudioChange()

virtual bool removeNotifyAudioChange ( AudioInfoSupport &  bi)
inlinevirtualinherited

Removes a target in order not to be notified about audio changes.

Reimplemented in RTSPClient< TcpClient, UdpSocket >.

◆ selfBLESerialClientESP32()

static BLESerialClientESP32 *& selfBLESerialClientESP32 ( )
inlinestaticprotected

◆ sendPacket()

bool sendPacket ( const uint8_t *  data,
size_t  len 
)
inlineprotectedinherited

◆ setAudioInfo()

void setAudioInfo ( AudioInfo  newInfo)
inlineoverridevirtualinherited

Defines the audio info and informs the other side if setAudioInfoActive(true) was called

Reimplemented from AudioStream.

◆ setAudioInfoActive()

void setAudioInfoActive ( bool  flag)
inlineinherited

Activates the exchange of the audio info.

◆ setAudioInfoUUID()

void setAudioInfoUUID ( const char *  uuid)
inlineinherited

Defines the UUID of the audio info characteristic.

◆ setConfirmWrite()

void setConfirmWrite ( bool  flag)
inlineinherited

Writes with response: slower but with flow control.

◆ setFramed()

void setFramed ( bool  flag)
inlineinherited

Keeps the write sizes: each write is one BLE packet.

◆ setHeader()

static void setHeader ( uint8_t *  packet,
int  len 
)
inlinestaticprotectedinherited

Copies the payload length into the 2 byte header.

◆ setMTU()

void setMTU ( int  mtu)
inlineinherited

Defines the requested MTU (must be called before begin)

◆ setNotifyActive()

void setNotifyActive ( bool  flag)
inlineinherited

Deactivate/Reactivate automatic AudioInfo updates: (default is active)

◆ setRxBufferSize()

void setRxBufferSize ( int  bytes,
int  packets = 100 
)
inlineinherited

Defines the size of the receive buffer and (in framed mode) the max number of packets: must be called before begin

◆ setRxUUID()

void setRxUUID ( const char *  uuid)
inlineinherited

Defines the UUID of the characteristic which is written by the client.

◆ setServiceUUID()

void setServiceUUID ( const char *  uuid)
inlineinherited

Defines the service UUID.

◆ setTxBufferSize()

void setTxBufferSize ( int  bytes,
int  packets = 100 
)
inlineinherited

Defines the size of the transmit buffer and (in framed mode) the max number of packets: must be called before begin

◆ setTxUUID()

void setTxUUID ( const char *  uuid)
inlineinherited

Defines the UUID of the characteristic which is read by the client.

◆ setWriteBufferSize()

void setWriteBufferSize ( int  size)
inlineinherited

◆ setWriteThrottle()

void setWriteThrottle ( int  ms)
inlineinherited

Waits the indicated ms after each write.

◆ startClient()

bool startClient ( const char *  localName)
inlineoverrideprotectedvirtual

Starts BLE and the scanning for the server.

Implements BLESerialClientBase.

◆ startScan()

void startScan ( )
inlineprotected

◆ toInfo()

AudioInfo toInfo ( const uint8_t *  data,
size_t  len 
)
inlineinherited

Converts the string from the info characteristic to the audio info.

◆ toStr()

StrView toStr ( AudioInfo  info)
inlineinherited

Converts the audio info to the string which is used in the info characteristic

◆ write() [1/2]

size_t write ( const uint8_t *  data,
size_t  len 
)
inlineoverridevirtualinherited

Reimplemented from AudioStream.

◆ write() [2/2]

virtual size_t write ( uint8_t  ch)
inlineoverridevirtualinherited

◆ writeAudioInfo()

void writeAudioInfo ( StrView  info)
inlineoverrideprotectedvirtual

Provides the audio info to the other side.

Implements BLESerialStream.

◆ writePacket()

bool writePacket ( const uint8_t *  data,
int  len 
)
inlineoverrideprotectedvirtual

Writes a packet to the server.

Implements BLESerialClientBase.

◆ writeSilence()

virtual void writeSilence ( size_t  len)
inlinevirtualinherited

Writes len bytes of silence (=0).

Member Data Documentation

◆ _timeout

int _timeout = 10
protectedinherited

◆ advertised_device

BLEAdvertisedDevice advertised_device
protected

◆ audio_info_str

char audio_info_str[40]
protectedinherited

◆ BLE_CH1_UUID

const char* BLE_CH1_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
protectedinherited

◆ BLE_CH2_UUID

const char* BLE_CH2_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
protectedinherited

◆ BLE_INFO_UUID

const char* BLE_INFO_UUID = "6e400004-b5a3-f393-e0a9-e50e24dcca9e"
protectedinherited

◆ BLE_SERIAL_SERVICE_UUID

const char* BLE_SERIAL_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e"
protectedinherited

◆ ch01_char

BLERemoteCharacteristic* ch01_char = nullptr
protected

◆ ch02_char

BLERemoteCharacteristic* ch02_char = nullptr
protected

◆ info

AudioInfo info
protectedinherited

◆ info_char

BLERemoteCharacteristic* info_char = nullptr
protected

◆ is_audio_info_active

bool is_audio_info_active = false
protectedinherited

◆ is_audio_info_defined

bool is_audio_info_defined = false
protectedinherited

◆ is_found

volatile bool is_found = false
protected

◆ is_framed

bool is_framed = false
protectedinherited

◆ is_notify_active

bool is_notify_active = true
protectedinherited

◆ is_setup

volatile bool is_setup = false
protected

◆ is_started

bool is_started = false
protectedinherited

◆ max_transfer_size

int max_transfer_size = 0
protectedinherited

◆ notify_vector

Vector<AudioInfoSupport*> notify_vector
protectedinherited

◆ p_client

BLEClient* p_client = nullptr
protected

◆ read_packet

Vector<uint8_t> read_packet {0}
protectedinherited

◆ rx_buffer

BLESerialPacketBuffer rx_buffer
protectedinherited

◆ rx_buffer_size

int rx_buffer_size = 4096
protectedinherited

◆ rx_mutex

Mutex rx_mutex
protected

◆ rx_packet_count

int rx_packet_count = 100
protectedinherited

◆ tmp_in

RingBuffer<uint8_t> tmp_in {0}
protectedinherited

◆ tmp_out

RingBuffer<uint8_t> tmp_out {0}
protectedinherited

◆ tx_buffer_size

int tx_buffer_size = 4096
protectedinherited

◆ tx_packet_count

int tx_packet_count = 100
protectedinherited

◆ write_buffer

SingleBuffer<uint8_t> write_buffer {0}
protectedinherited

◆ write_buffer_size

int write_buffer_size = MAX_SINGLE_CHARS
protectedinherited

◆ write_confirmation_flag

bool write_confirmation_flag = false
protectedinherited

◆ write_throttle

int write_throttle = 0
protectedinherited

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