From c3ec91f9683954e567ebddc77312c004ef42c933 Mon Sep 17 00:00:00 2001 From: Luc Date: Mon, 19 Nov 2018 19:30:28 +0100 Subject: [PATCH] Allow to add custom callback in BT Serial (#2081) This allow to catch the events when connected /disconnected,, etc... This also allow to get parameters of events like the remote address of connected devices, etc... Small change but lot of flexibility --- libraries/BluetoothSerial/src/BluetoothSerial.cpp | 8 ++++++++ libraries/BluetoothSerial/src/BluetoothSerial.h | 2 ++ 2 files changed, 10 insertions(+) diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.cpp b/libraries/BluetoothSerial/src/BluetoothSerial.cpp index 001be966..a8332b8b 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.cpp +++ b/libraries/BluetoothSerial/src/BluetoothSerial.cpp @@ -51,6 +51,7 @@ static SemaphoreHandle_t _spp_tx_done = NULL; 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; #define SPP_RUNNING 0x01 #define SPP_CONNECTED 0x02 @@ -231,6 +232,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) default: break; } + if(custom_spp_callback)(*custom_spp_callback)(event, param); } static bool _init_bt(const char *deviceName) @@ -437,4 +439,10 @@ void BluetoothSerial::end() _stop_bt(); } +esp_err_t BluetoothSerial::register_callback(esp_spp_cb_t * callback) +{ + custom_spp_callback = callback; + return ESP_OK; +} + #endif diff --git a/libraries/BluetoothSerial/src/BluetoothSerial.h b/libraries/BluetoothSerial/src/BluetoothSerial.h index adf6df66..3f4372e5 100644 --- a/libraries/BluetoothSerial/src/BluetoothSerial.h +++ b/libraries/BluetoothSerial/src/BluetoothSerial.h @@ -21,6 +21,7 @@ #include "Arduino.h" #include "Stream.h" +#include class BluetoothSerial: public Stream { @@ -38,6 +39,7 @@ class BluetoothSerial: public Stream size_t write(const uint8_t *buffer, size_t size); void flush(); void end(void); + esp_err_t register_callback(esp_spp_cb_t * callback); private: String local_name;