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

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+
staticesp_spp_cb_t * custom_spp_callback = NULL;
5455

5556
#defineSPP_RUNNING0x01
5657
#defineSPP_CONNECTED0x02
@@ -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
staticbool_init_bt(constchar *deviceName)
@@ -437,4 +439,10 @@ void BluetoothSerial::end()
437439
_stop_bt();
438440
}
439441

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

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
classBluetoothSerial: publicStream
2627
{
@@ -38,6 +39,7 @@ class BluetoothSerial: public Stream
3839
size_twrite(constuint8_t *buffer, size_t size);
3940
voidflush();
4041
voidend(void);
42+
esp_err_tregister_callback(esp_spp_cb_t * callback);
4143

4244
private:
4345
String local_name;

0 commit comments

Comments
 (0)
close