Skip to content

Commit 307b136

Browse files
authored
Implement BTSerial onData to dynamically receive packets from SPP (#3649)
1 parent 32d5654 commit 307b136

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636
#include "esp_spp_api.h"
3737
#include <esp_log.h>
3838

39-
#ifdef ARDUINO_ARCH_ESP32
4039
#include "esp32-hal-log.h"
41-
#endif
4240

4341
const char * _spp_server_name = "ESP32SPP";
4442

@@ -52,6 +50,7 @@ static TaskHandle_t _spp_task_handle = NULL;
5250
static EventGroupHandle_t _spp_event_group = NULL;
5351
static boolean secondConnectionAttempt;
5452
static esp_spp_cb_t * custom_spp_callback = NULL;
53+
static BluetoothSerialDataCb custom_data_callback = NULL;
5554

5655
#define INQ_LEN 0x10
5756
#define INQ_NUM_RSPS 20
@@ -218,7 +217,6 @@ static void _spp_tx_task(void * arg){
218217
_spp_task_handle = NULL;
219218
}
220219

221-
222220
static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
223221
{
224222
switch (event)
@@ -278,7 +276,9 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
278276
//esp_log_buffer_hex("",param->data_ind.data,param->data_ind.len); //for low level debug
279277
//ets_printf("r:%u\n", param->data_ind.len);
280278

281-
if (_spp_rx_queue != NULL){
279+
if(custom_data_callback){
280+
custom_data_callback(param->data_ind.data, param->data_ind.len);
281+
} else if (_spp_rx_queue != NULL){
282282
for (int i = 0; i < param->data_ind.len; i++){
283283
if(xQueueSend(_spp_rx_queue, param->data_ind.data + i, (TickType_t)0) != pdTRUE){
284284
log_e("RX Full! Discarding %u bytes", param->data_ind.len - i);
@@ -322,6 +322,10 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
322322
if(custom_spp_callback)(*custom_spp_callback)(event, param);
323323
}
324324

325+
void BluetoothSerial::onData(BluetoothSerialDataCb cb){
326+
custom_data_callback = cb;
327+
}
328+
325329
static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
326330
{
327331
switch(event){
@@ -469,7 +473,7 @@ static bool _init_bt(const char *deviceName)
469473
}
470474

471475
if(!_spp_task_handle){
472-
xTaskCreate(_spp_tx_task, "spp_tx", 4096, NULL, 2, &_spp_task_handle);
476+
xTaskCreatePinnedToCore(_spp_tx_task, "spp_tx", 4096, NULL, 2, &_spp_task_handle, 0);
473477
if(!_spp_task_handle){
474478
log_e("Network Event Task Start Failed!");
475479
return false;
@@ -511,6 +515,10 @@ static bool _init_bt(const char *deviceName)
511515
return false;
512516
}
513517

518+
if (esp_bt_sleep_disable() != ESP_OK){
519+
log_e("esp_bt_sleep_disable failed");
520+
}
521+
514522
log_i("device name set");
515523
esp_bt_dev_set_device_name(deviceName);
516524

Diff for: libraries/BluetoothSerial/src/BluetoothSerial.h

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include "Arduino.h"
2323
#include "Stream.h"
2424
#include <esp_spp_api.h>
25+
#include <functional>
26+
27+
typedef std::function<void(const uint8_t *buffer, size_t size)> BluetoothSerialDataCb;
2528

2629
class BluetoothSerial: public Stream
2730
{
@@ -39,6 +42,7 @@ class BluetoothSerial: public Stream
3942
size_t write(const uint8_t *buffer, size_t size);
4043
void flush();
4144
void end(void);
45+
void onData(BluetoothSerialDataCb cb);
4246
esp_err_t register_callback(esp_spp_cb_t * callback);
4347

4448
void enableSSP();

0 commit comments

Comments
 (0)