diff --git a/_audio_b_l_e_client_8h_source.html b/_audio_b_l_e_client_8h_source.html index d8ea132c05..97c2a41171 100644 --- a/_audio_b_l_e_client_8h_source.html +++ b/_audio_b_l_e_client_8h_source.html @@ -83,147 +83,174 @@
12 static AudioBLEClient *selfAudioBLEClient = nullptr;
13 
22 class AudioBLEClient : public AudioBLEStream,
-
23  public BLEAdvertisedDeviceCallbacks {
-
24 public:
-
25  AudioBLEClient(int mtu = BLE_BUFFER_SIZE) : AudioBLEStream(mtu) {
-
26  selfAudioBLEClient = this;
-
27  }
-
28 
-
30  bool begin(const char *serverName, int seconds) {
-
31  TRACEI();
-
32  ble_server_name = serverName;
-
33  // Init BLE device
-
34  BLEDevice::init("client");
-
35 
-
36  // Retrieve a Scanner and set the callback we want to use to be informed
-
37  // when we have detected a new device.
-
38  BLEScan *pBLEScan = BLEDevice::getScan();
-
39  pBLEScan->setAdvertisedDeviceCallbacks(this);
-
40  pBLEScan->setActiveScan(true);
-
41  pBLEScan->start(seconds);
-
42  return true;
-
43  }
-
44 
-
45  void end() override {
-
46  TRACEI();
-
47  flush();
-
48  BLEDevice::deinit();
-
49  }
-
50 
-
51  size_t readBytes(uint8_t *data, size_t dataSize) override {
-
52  TRACED();
-
53  // changed to auto to be version independent (it changed from std::string to String)
-
54  auto str = ch02_char->readValue();
-
55  memcpy(data, str.c_str(), str.length());
-
56  return str.length();
-
57  }
-
58 
-
59  int available() override { return BLE_BUFFER_SIZE; }
-
60 
-
61  size_t write(const uint8_t *data, size_t dataSize) override {
-
62  ch01_char->writeValue((uint8_t *)data, dataSize, false);
-
63  return dataSize;
-
64  }
-
65 
-
66  int availableForWrite() override { return BLE_BUFFER_SIZE; }
-
67 
-
68  virtual bool connected() override { return is_client_connected; }
-
69 
-
70 protected:
-
71  // client
-
72  BLEClient *p_client = nullptr;
-
73  BLEAdvertising *p_advertising = nullptr;
-
74  BLERemoteService *p_remote_service = nullptr;
-
75  BLEAddress *p_server_address = nullptr;
-
76  BLERemoteCharacteristic *ch01_char = nullptr;
-
77  BLERemoteCharacteristic *ch02_char = nullptr;
-
78  BLERemoteCharacteristic *info_char = nullptr;
-
79  BLEAdvertisedDevice advertised_device;
-
80  bool is_client_connected = false;
-
81 
-
82  void writeAudioInfoCharacteristic(AudioInfo info) override {
-
83  TRACEI();
-
84  // send update via BLE
-
85  info_char->writeValue((uint8_t *)&info, sizeof(AudioInfo));
-
86  }
-
87 
-
88  // Scanning Results
-
89  void onResult(BLEAdvertisedDevice advertisedDevice) override {
-
90  TRACEI();
-
91  // Check if the name of the advertiser matches
-
92  if (advertisedDevice.getName() == ble_server_name) {
-
93  TRACEI();
-
94  advertised_device = advertisedDevice;
-
95  // Scan can be stopped, we found what we are looking for
-
96  advertised_device.getScan()->stop();
-
97  // Address of advertiser is the one we need
-
98  // p_server_address = new BLEAddress(advertisedDevice.getAddress());
-
99 
-
100  LOGI("Device '%s' found: Connecting!",
-
101  advertised_device.toString().c_str());
-
102  setupBLEClient();
-
103  }
-
104  delay(10);
-
105  }
-
106 
-
107  static void notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic,
-
108  uint8_t *pData, size_t length, bool isNotify) {
-
109  TRACEI();
-
110  if (pBLERemoteCharacteristic->getUUID().toString() ==
-
111  selfAudioBLEClient->BLE_INFO_UUID) {
-
112  selfAudioBLEClient->setAudioInfo(pData, length);
-
113  }
-
114  }
-
115 
-
116  bool setupBLEClient() {
-
117  TRACEI();
-
118 
-
119  if (p_client == nullptr)
-
120  p_client = BLEDevice::createClient();
-
121 
-
122  // Connect to the remove BLE Server.
-
123  LOGI("Connecting to %s ...",
-
124  advertised_device.getAddress().toString().c_str());
-
125  // p_client->connect(advertised_device.getAddress(),BLE_ADDR_TYPE_RANDOM);
-
126  p_client->connect(&advertised_device);
-
127  if (!p_client->isConnected()) {
-
128  LOGE("connect failed");
-
129  return false;
-
130  }
-
131  p_client->setMTU(max_transfer_size);
-
132 
-
133  LOGI("Connected to server: %s", is_client_connected ? "true" : "false");
-
134 
-
135  // Obtain a reference to the service we are after in the remote BLE
-
136  // server.
-
137  if (p_remote_service == nullptr) {
-
138  p_remote_service = p_client->getService(BLE_SERIAL_SERVICE_UUID);
-
139  if (p_remote_service == nullptr) {
-
140  LOGE("Failed to find our service UUID: %s", BLE_SERIAL_SERVICE_UUID);
-
141  return (false);
-
142  }
-
143  }
+
23  public BLEClientCallbacks,
+
24  public BLEAdvertisedDeviceCallbacks {
+
25 public:
+
26  AudioBLEClient(int mtu = BLE_BUFFER_SIZE) : AudioBLEStream(mtu) {
+
27  selfAudioBLEClient = this;
+
28  }
+
29 
+
30 
+
31 
+
33  bool begin(const char *serverName, int seconds) {
+
34  TRACEI();
+
35  ble_server_name = serverName;
+
36  // Init BLE device
+
37  BLEDevice::init("client");
+
38 
+
39  // Retrieve a Scanner and set the callback we want to use to be informed
+
40  // when we have detected a new device.
+
41  BLEScan *pBLEScan = BLEDevice::getScan();
+
42  pBLEScan->setAdvertisedDeviceCallbacks(this);
+
43  pBLEScan->setActiveScan(true);
+
44  pBLEScan->start(seconds);
+
45  return true;
+
46  }
+
47 
+
48  void end() override {
+
49  TRACEI();
+
50  flush();
+
51  BLEDevice::deinit();
+
52  }
+
53 
+
54  size_t readBytes(uint8_t *data, size_t dataSize) override {
+
55  TRACED();
+
56  if (!is_client_connected || !is_client_set_up) return 0;
+
57  if (!ch01_char->canRead()) return 0;
+
58  // changed to auto to be version independent (it changed from std::string to
+
59  // String)
+
60  auto str = ch01_char->readValue();
+
61  if (str.length() > 0){
+
62  memcpy(data, str.c_str(), str.length());
+
63  }
+
64  return str.length();
+
65  }
+
66 
+
67  int available() override { return BLE_BUFFER_SIZE; }
+
68 
+
69  size_t write(const uint8_t *data, size_t dataSize) override {
+
70  int result = 0;
+
71  if (!is_client_connected || !is_client_set_up) return 0;
+
72  if (ch02_char->canWrite()) {
+
73  ch02_char->writeValue((uint8_t *)data, dataSize, false);
+
74  result = dataSize;
+
75  }
+
76  return result;
+
77  }
+
78 
+
79  int availableForWrite() override { return BLE_BUFFER_SIZE; }
+
80 
+
81  virtual bool connected() override { return is_client_connected; }
+
82 
+
83 protected:
+
84  // client
+
85  BLEClient *p_client = nullptr;
+
86  BLEAdvertising *p_advertising = nullptr;
+
87  BLERemoteService *p_remote_service = nullptr;
+
88  BLEAddress *p_server_address = nullptr;
+
89  BLERemoteCharacteristic *ch01_char = nullptr; // read
+
90  BLERemoteCharacteristic *ch02_char = nullptr; // write
+
91  BLERemoteCharacteristic *info_char = nullptr;
+
92  BLEAdvertisedDevice advertised_device;
+
93  volatile bool is_client_connected = false;
+
94  bool is_client_set_up = false;
+
95 
+
96  virtual void onConnect(BLEClient *pClient) {
+
97  TRACEI();
+
98  is_client_connected = true;
+
99  }
+
100  virtual void onDisconnect(BLEClient *pClient) {
+
101  TRACEI();
+
102  is_client_connected = false;
+
103  };
+
104 
+
105  void writeAudioInfoCharacteristic(AudioInfo info) override {
+
106  TRACEI();
+
107  // send update via BLE
+
108  info_char->writeValue((uint8_t *)&info, sizeof(AudioInfo));
+
109  }
+
110 
+
111  // Scanning Results
+
112  void onResult(BLEAdvertisedDevice advertisedDevice) override {
+
113  TRACEI();
+
114  // Check if the name of the advertiser matches
+
115  if (advertisedDevice.getName() == ble_server_name) {
+
116  TRACEI();
+
117  advertised_device = advertisedDevice;
+
118  // Scan can be stopped, we found what we are looking for
+
119  advertised_device.getScan()->stop();
+
120  // Address of advertiser is the one we need
+
121  // p_server_address = new BLEAddress(advertisedDevice.getAddress());
+
122 
+
123  LOGI("Device '%s' found: Connecting!",
+
124  advertised_device.toString().c_str());
+
125  setupBLEClient();
+
126  }
+
127  delay(10);
+
128  }
+
129 
+
130  static void notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic,
+
131  uint8_t *pData, size_t length, bool isNotify) {
+
132  TRACEI();
+
133  if (pBLERemoteCharacteristic->getUUID().toString() ==
+
134  selfAudioBLEClient->BLE_INFO_UUID) {
+
135  selfAudioBLEClient->setAudioInfo(pData, length);
+
136  }
+
137  }
+
138 
+
139  bool setupBLEClient() {
+
140  TRACEI();
+
141 
+
142  if (p_client == nullptr)
+
143  p_client = BLEDevice::createClient();
144 
-
145  if (ch01_char == nullptr) {
-
146  ch01_char = p_remote_service->getCharacteristic(BLE_CH1_UUID);
-
147  }
-
148 
-
149  if (ch02_char == nullptr) {
-
150  ch02_char= p_remote_service->getCharacteristic(BLE_CH2_UUID);
-
151  }
-
152 
-
153  if (is_audio_info_active && info_char == nullptr) {
-
154  info_char = p_remote_service->getCharacteristic(BLE_INFO_UUID);
-
155  info_char->registerForNotify(notifyCallback);
+
145  // onConnect and on onDisconnect support
+
146  p_client->setClientCallbacks(this);
+
147 
+
148  // Connect to the remove BLE Server.
+
149  LOGI("Connecting to %s ...",
+
150  advertised_device.getAddress().toString().c_str());
+
151  // p_client->connect(advertised_device.getAddress(),BLE_ADDR_TYPE_RANDOM);
+
152  p_client->connect(&advertised_device);
+
153  if (!p_client->isConnected()) {
+
154  LOGE("connect failed");
+
155  return false;
156  }
-
157  is_client_connected = true;
-
158  return is_client_connected;
-
159  }
-
160 };
-
161 
-
162 } // namespace audio_tools
-
audio_tools::AudioBLEClient
A simple BLE client that implements the serial protocol, so that it can be used to send and recevie a...
Definition: AudioBLEClient.h:23
-
audio_tools::AudioBLEClient::begin
bool begin(const char *serverName, int seconds)
starts a BLE client
Definition: AudioBLEClient.h:30
+
157 
+
158  LOGI("Setting mtu to %d", max_transfer_size);
+
159  p_client->setMTU(max_transfer_size);
+
160 
+
161  // Obtain a reference to the service we are after in the remote BLE
+
162  // server.
+
163  if (p_remote_service == nullptr) {
+
164  p_remote_service = p_client->getService(BLE_SERIAL_SERVICE_UUID);
+
165  if (p_remote_service == nullptr) {
+
166  LOGE("Failed to find our service UUID: %s", BLE_SERIAL_SERVICE_UUID);
+
167  return (false);
+
168  }
+
169  }
+
170 
+
171  if (ch01_char == nullptr) {
+
172  ch01_char = p_remote_service->getCharacteristic(BLE_CH1_UUID);
+
173  }
+
174 
+
175  if (ch02_char == nullptr) {
+
176  ch02_char = p_remote_service->getCharacteristic(BLE_CH2_UUID);
+
177  }
+
178 
+
179  if (is_audio_info_active && info_char == nullptr) {
+
180  info_char = p_remote_service->getCharacteristic(BLE_INFO_UUID);
+
181  info_char->registerForNotify(notifyCallback);
+
182  }
+
183  LOGI("Connected to server: %s", is_client_connected ? "true" : "false");
+
184  is_client_set_up = true;
+
185  return is_client_connected;
+
186  }
+
187 };
+
188 
+
189 } // namespace audio_tools
+
audio_tools::AudioBLEClient
A simple BLE client that implements the serial protocol, so that it can be used to send and recevie a...
Definition: AudioBLEClient.h:24
+
audio_tools::AudioBLEClient::begin
bool begin(const char *serverName, int seconds)
starts a BLE client
Definition: AudioBLEClient.h:33
audio_tools::AudioBLEStream
Transmit and receive data via BLE using a Serial API. The following additional experimental features ...
Definition: AudioBLEStream.h:24
audio_tools
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
audio_tools::delay
void delay(uint32_t ms)
Waits for the indicated milliseconds.
Definition: Millis.h:10
diff --git a/classaudio__tools_1_1_audio_b_l_e_client-members.html b/classaudio__tools_1_1_audio_b_l_e_client-members.html index 4ffad39a4e..f22aad2af9 100644 --- a/classaudio__tools_1_1_audio_b_l_e_client-members.html +++ b/classaudio__tools_1_1_audio_b_l_e_client-members.html @@ -100,43 +100,46 @@ info_char (defined in AudioBLEClient)AudioBLEClientprotected is_audio_info_active (defined in AudioBLEStream)AudioBLEStreamprotected is_client_connected (defined in AudioBLEClient)AudioBLEClientprotected - is_framed (defined in AudioBLEStream)AudioBLEStreamprotected - is_started (defined in AudioBLEStream)AudioBLEStreamprotected - max_transfer_size (defined in AudioBLEStream)AudioBLEStreamprotected - not_supported(int out, const char *msg="") (defined in AudioStream)AudioStreaminlineprotectedvirtual - notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify) (defined in AudioBLEClient)AudioBLEClientinlineprotectedstatic - onResult(BLEAdvertisedDevice advertisedDevice) override (defined in AudioBLEClient)AudioBLEClientinlineprotected - operator bool() (defined in AudioBLEStream)AudioBLEStreaminline - operator=(AudioBLEStream const &other)=delete (defined in AudioBLEStream)AudioBLEStreamprotected - operator=(AudioStream const &)=delete (defined in AudioStream)AudioStream - p_advertising (defined in AudioBLEClient)AudioBLEClientprotected - p_client (defined in AudioBLEClient)AudioBLEClientprotected - p_notify (defined in AudioStream)AudioStreamprotected - p_remote_service (defined in AudioBLEClient)AudioBLEClientprotected - p_server_address (defined in AudioBLEClient)AudioBLEClientprotected - readBytes(uint8_t *data, size_t dataSize) override (defined in AudioBLEClient)AudioBLEClientinlinevirtual - readSilence(uint8_t *buffer, size_t length)AudioStreaminlinevirtual - refillReadBuffer() (defined in AudioStream)AudioStreaminlineprotected - setAudioInfo(AudioInfo info) (defined in AudioBLEStream)AudioBLEStreaminlinevirtual - setAudioInfo(const uint8_t *data, size_t size) (defined in AudioBLEStream)AudioBLEStreaminlineprotectedvirtual - setAudioInfoActive(bool flag) (defined in AudioBLEStream)AudioBLEStreaminline - setAudioInfoUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline - setFramed(bool flag) (defined in AudioBLEStream)AudioBLEStreaminline - setNotifyAudioChange(AudioInfoSupport &bi) override (defined in AudioStream)AudioStreaminlinevirtual - setRxUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline - setServiceUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline - setTxUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline - setupBLEClient() (defined in AudioBLEClient)AudioBLEClientinlineprotected - tmp_in (defined in AudioStream)AudioStreamprotected - tmp_out (defined in AudioStream)AudioStreamprotected - toInfo(const uint8_t *str) (defined in AudioBLEStream)AudioBLEStreaminline - toStr(AudioInfo info) (defined in AudioBLEStream)AudioBLEStreaminline - validate(AudioInfo &info) (defined in AudioInfoSupport)AudioInfoSupportinlinevirtual - write(const uint8_t *data, size_t dataSize) override (defined in AudioBLEClient)AudioBLEClientinlinevirtual - write(uint8_t ch) override (defined in AudioStream)AudioStreaminlinevirtual - writeAudioInfoCharacteristic(AudioInfo info) override (defined in AudioBLEClient)AudioBLEClientinlineprotectedvirtual - writeSilence(size_t len)AudioStreaminlinevirtual - ~AudioStream()=default (defined in AudioStream)AudioStreamvirtual + is_client_set_up (defined in AudioBLEClient)AudioBLEClientprotected + is_framed (defined in AudioBLEStream)AudioBLEStreamprotected + is_started (defined in AudioBLEStream)AudioBLEStreamprotected + max_transfer_size (defined in AudioBLEStream)AudioBLEStreamprotected + not_supported(int out, const char *msg="") (defined in AudioStream)AudioStreaminlineprotectedvirtual + notifyCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify) (defined in AudioBLEClient)AudioBLEClientinlineprotectedstatic + onConnect(BLEClient *pClient) (defined in AudioBLEClient)AudioBLEClientinlineprotectedvirtual + onDisconnect(BLEClient *pClient) (defined in AudioBLEClient)AudioBLEClientinlineprotectedvirtual + onResult(BLEAdvertisedDevice advertisedDevice) override (defined in AudioBLEClient)AudioBLEClientinlineprotected + operator bool() (defined in AudioBLEStream)AudioBLEStreaminline + operator=(AudioBLEStream const &other)=delete (defined in AudioBLEStream)AudioBLEStreamprotected + operator=(AudioStream const &)=delete (defined in AudioStream)AudioStream + p_advertising (defined in AudioBLEClient)AudioBLEClientprotected + p_client (defined in AudioBLEClient)AudioBLEClientprotected + p_notify (defined in AudioStream)AudioStreamprotected + p_remote_service (defined in AudioBLEClient)AudioBLEClientprotected + p_server_address (defined in AudioBLEClient)AudioBLEClientprotected + readBytes(uint8_t *data, size_t dataSize) override (defined in AudioBLEClient)AudioBLEClientinlinevirtual + readSilence(uint8_t *buffer, size_t length)AudioStreaminlinevirtual + refillReadBuffer() (defined in AudioStream)AudioStreaminlineprotected + setAudioInfo(AudioInfo info) (defined in AudioBLEStream)AudioBLEStreaminlinevirtual + setAudioInfo(const uint8_t *data, size_t size) (defined in AudioBLEStream)AudioBLEStreaminlineprotectedvirtual + setAudioInfoActive(bool flag) (defined in AudioBLEStream)AudioBLEStreaminline + setAudioInfoUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline + setFramed(bool flag) (defined in AudioBLEStream)AudioBLEStreaminline + setNotifyAudioChange(AudioInfoSupport &bi) override (defined in AudioStream)AudioStreaminlinevirtual + setRxUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline + setServiceUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline + setTxUUID(const char *uuid) (defined in AudioBLEStream)AudioBLEStreaminline + setupBLEClient() (defined in AudioBLEClient)AudioBLEClientinlineprotected + tmp_in (defined in AudioStream)AudioStreamprotected + tmp_out (defined in AudioStream)AudioStreamprotected + toInfo(const uint8_t *str) (defined in AudioBLEStream)AudioBLEStreaminline + toStr(AudioInfo info) (defined in AudioBLEStream)AudioBLEStreaminline + validate(AudioInfo &info) (defined in AudioInfoSupport)AudioInfoSupportinlinevirtual + write(const uint8_t *data, size_t dataSize) override (defined in AudioBLEClient)AudioBLEClientinlinevirtual + write(uint8_t ch) override (defined in AudioStream)AudioStreaminlinevirtual + writeAudioInfoCharacteristic(AudioInfo info) override (defined in AudioBLEClient)AudioBLEClientinlineprotectedvirtual + writeSilence(size_t len)AudioStreaminlinevirtual + ~AudioStream()=default (defined in AudioStream)AudioStreamvirtual