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
4341const  char  * _spp_server_name = " ESP32SPP" 
4442
@@ -52,6 +50,7 @@ static TaskHandle_t _spp_task_handle = NULL;
5250static  EventGroupHandle_t _spp_event_group = NULL ;
5351static  boolean secondConnectionAttempt;
5452static  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- 
222220static  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" 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+ 
325329static  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
0 commit comments