Skip to content

Commit c3ec91f

Browse files
Lucme-no-dev
Luc
authored andcommitted
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
1 parent a300059 commit c3ec91f

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static SemaphoreHandle_t _spp_tx_done = NULL;
5151
static TaskHandle_t _spp_task_handle = NULL;
5252
static EventGroupHandle_t _spp_event_group = NULL;
5353
static boolean secondConnectionAttempt;
54+
static esp_spp_cb_t * custom_spp_callback = NULL;
5455

5556
#define SPP_RUNNING 0x01
5657
#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)
231232
default:
232233
break;
233234
}
235+
if(custom_spp_callback)(*custom_spp_callback)(event, param);
234236
}
235237

236238
static bool _init_bt(const char *deviceName)
@@ -437,4 +439,10 @@ void BluetoothSerial::end()
437439
_stop_bt();
438440
}
439441

442+
esp_err_t BluetoothSerial::register_callback(esp_spp_cb_t * callback)
443+
{
444+
custom_spp_callback = callback;
445+
return ESP_OK;
446+
}
447+
440448
#endif

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

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "Arduino.h"
2323
#include "Stream.h"
24+
#include <esp_spp_api.h>
2425

2526
class BluetoothSerial: public Stream
2627
{
@@ -38,6 +39,7 @@ class BluetoothSerial: public Stream
3839
size_t write(const uint8_t *buffer, size_t size);
3940
void flush();
4041
void end(void);
42+
esp_err_t register_callback(esp_spp_cb_t * callback);
4143

4244
private:
4345
String local_name;

0 commit comments

Comments
 (0)