arduino-audio-tools
AudioBLEStream.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
5 #include "AudioTools/CoreAudio/Buffers.h"
6 #include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7 
8 
9 namespace audio_tools {
10 
19 class AudioBLEStream : public AudioStream {
20 public:
21  AudioBLEStream(int defaultMTU) { max_transfer_size = defaultMTU; };
22 
23  virtual void end() = 0;
24 
25  virtual bool connected() = 0;
26 
27  void setAudioInfo(AudioInfo info) {
28  if (is_audio_info_active && this->info != info) {
29  TRACED();
31  writeAudioInfoCharacteristic(info);
32  }
33  }
34 
35  operator bool() { return connected(); }
36 
37  void setServiceUUID(const char *uuid) { BLE_AUDIO_SERVICE_UUID = uuid; }
38 
39  void setRxUUID(const char *uuid) { BLE_CH2_UUID = uuid; }
40 
41  void setTxUUID(const char *uuid) { BLE_CH1_UUID = uuid; }
42 
43  void setAudioInfoUUID(const char *uuid) { BLE_INFO_UUID = uuid; }
44 
45  void setAudioInfoActive(bool flag) { is_audio_info_active = flag; }
46 
47  void setFramed(bool flag) { is_framed = flag; }
48 
49  StrView toStr(AudioInfo info) {
50  snprintf(audio_info_str, 40, "%d:%d:%d", info.sample_rate, info.channels,
51  info.bits_per_sample);
52  return StrView(audio_info_str);
53  }
54 
55  AudioInfo toInfo(const uint8_t *str) {
56  AudioInfo result;
57  sscanf((char*)str,"%d:%d:%d", &result.sample_rate, &result.channels, &result.bits_per_sample);
58  return result;
59  }
60 
61 protected:
62  // disable copy constructor
63  AudioBLEStream(AudioBLEStream const &other) = delete;
64  // disable assign constructor
65  void operator=(AudioBLEStream const &other) = delete;
66  const char *ble_server_name = nullptr;
67  uint16_t max_transfer_size = 0;
68  bool is_started = false;
69  bool is_audio_info_active = false;
70  bool is_framed = false;
71  char audio_info_str[40];
72 
73  // Bluetooth LE GATT UUIDs for the Nordic UART profile Change UUID here if
74  // required
75  const char *BLE_AUDIO_SERVICE_UUID = "6e400001-b5a3-f393-e0a9-e50e24dcca9e";
76  const char *BLE_CH1_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"; // RX
77  const char *BLE_CH2_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"; // TX
78  const char *BLE_INFO_UUID = "6e400004-b5a3-f393-e0a9-e50e24dcca9e";
79 
80  virtual int getMTU() = 0;
81 
82  // override to implement your own extended logic
83  virtual void setAudioInfo(const uint8_t *data, size_t size) {
84  if (is_audio_info_active) {
85  AudioInfo ai = toInfo(data);
86  setAudioInfo(ai);
87  }
88  }
89  // override to implement your own extended logic
90  virtual void writeAudioInfoCharacteristic(AudioInfo info) = 0;
91 };
92 
93 } // namespace audio_tools
Transmit and receive data via BLE using a Serial API. The following additional experimental features ...
Definition: AudioBLEStream.h:19
void setAudioInfo(AudioInfo info)
Defines the input AudioInfo.
Definition: AudioBLEStream.h:27
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition: BaseStream.h:109
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition: BaseStream.h:117
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52