From 307b1368dd3688f892d0e75236cb393a3796590f Mon Sep 17 00:00:00 2001 From: Me No Dev Date: Mon, 20 Jan 2020 19:43:52 +0200 Subject: [PATCH] Implement BTSerial onData to dynamically receive packets from SPP (#3649) --- .../BluetoothSerial/src/BluetoothSerial.cpp | 18 +++++++++++++----- .../BluetoothSerial/src/BluetoothSerial.h | 4 ++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index 88cfad06..5b554f0d 100755 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -36,9 +36,7 @@ #include "esp_spp_api.h" #include -#ifdef ARDUINO_ARCH_ESP32 #include "esp32-hal-log.h" -#endif const char * _spp_server_name = "ESP32SPP"; @@ -52,6 +50,7 @@ static TaskHandle_t _spp_task_handle = NULL; static EventGroupHandle_t _spp_event_group = NULL; static boolean secondConnectionAttempt; static esp_spp_cb_t * custom_spp_callback = NULL; +static BluetoothSerialDataCb custom_data_callback = NULL; #define INQ_LEN 0x10 #define INQ_NUM_RSPS 20 @@ -218,7 +217,6 @@ static void _spp_tx_task(void * arg){ _spp_task_handle = NULL; } - static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) { switch (event) @@ -278,7 +276,9 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) //esp_log_buffer_hex("",param->data_ind.data,param->data_ind.len); //for low level debug //ets_printf("r:%u\n", param->data_ind.len); - if (_spp_rx_queue != NULL){ + if(custom_data_callback){ + custom_data_callback(param->data_ind.data, param->data_ind.len); + } else if (_spp_rx_queue != NULL){ for (int i = 0; i < param->data_ind.len; i++){ if(xQueueSend(_spp_rx_queue, param->data_ind.data + i, (TickType_t)0) != pdTRUE){ 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) if(custom_spp_callback)(*custom_spp_callback)(event, param); } +void BluetoothSerial::onData(BluetoothSerialDataCb cb){ + custom_data_callback = cb; +} + static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) { switch(event){ @@ -469,7 +473,7 @@ static bool _init_bt(const char *deviceName) } if(!_spp_task_handle){ - xTaskCreate(_spp_tx_task, "spp_tx", 4096, NULL, 2, &_spp_task_handle); + xTaskCreatePinnedToCore(_spp_tx_task, "spp_tx", 4096, NULL, 2, &_spp_task_handle, 0); if(!_spp_task_handle){ log_e("Network Event Task Start Failed!"); return false; @@ -511,6 +515,10 @@ static bool _init_bt(const char *deviceName) return false; } + if (esp_bt_sleep_disable() != ESP_OK){ + log_e("esp_bt_sleep_disable failed"); + } + log_i("device name set"); esp_bt_dev_set_device_name(deviceName); diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.h b/libraries/BluetoothSerial/src/BluetoothSerial.h index 68269fa9..6ccb9b49 100755 --- a/libraries/BluetoothSerial/src/BluetoothSerial.h +++ b/libraries/BluetoothSerial/src/BluetoothSerial.h @@ -22,6 +22,9 @@ #include "Arduino.h" #include "Stream.h" #include +#include + +typedef std::function BluetoothSerialDataCb; class BluetoothSerial: public Stream { @@ -39,6 +42,7 @@ class BluetoothSerial: public Stream size_t write(const uint8_t *buffer, size_t size); void flush(); void end(void); + void onData(BluetoothSerialDataCb cb); esp_err_t register_callback(esp_spp_cb_t * callback); void enableSSP();