Skip to content

Commit 81b9130

Browse files
authored
BluetoothSerial SSP Authentication with callbacks (espressif#4634)
Added authentication callbacks and example, resolves espressif#4622.
1 parent 434d02c commit 81b9130

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//This example code is in the Public Domain (or CC0 licensed, at your option.)
2+
//By Richard Li - 2020
3+
//
4+
//This example creates a bridge between Serial and Classical Bluetooth (SPP with authentication)
5+
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
6+
7+
#include "BluetoothSerial.h"
8+
9+
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
10+
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
11+
#endif
12+
13+
BluetoothSerial SerialBT;
14+
boolean confirmRequestPending = true;
15+
16+
void BTConfirmRequestCallback(uint32_t numVal)
17+
{
18+
confirmRequestPending = true;
19+
Serial.println(numVal);
20+
}
21+
22+
void BTAuthCompleteCallback(boolean success)
23+
{
24+
confirmRequestPending = false;
25+
if (success)
26+
{
27+
Serial.println("Pairing success!!");
28+
}
29+
else
30+
{
31+
Serial.println("Pairing failed, rejected by user!!");
32+
}
33+
}
34+
35+
36+
void setup()
37+
{
38+
Serial.begin(115200);
39+
SerialBT.enableSSP();
40+
SerialBT.onConfirmRequest(BTConfirmRequestCallback);
41+
SerialBT.onAuthComplete(BTAuthCompleteCallback);
42+
SerialBT.begin("ESP32test"); //Bluetooth device name
43+
Serial.println("The device started, now you can pair it with bluetooth!");
44+
}
45+
46+
void loop()
47+
{
48+
if (confirmRequestPending)
49+
{
50+
if (Serial.available())
51+
{
52+
int dat = Serial.read();
53+
if (dat == 'Y' || dat == 'y')
54+
{
55+
SerialBT.confirmReply(true);
56+
}
57+
else
58+
{
59+
SerialBT.confirmReply(false);
60+
}
61+
}
62+
}
63+
else
64+
{
65+
if (Serial.available())
66+
{
67+
SerialBT.write(Serial.read());
68+
}
69+
if (SerialBT.available())
70+
{
71+
Serial.write(SerialBT.read());
72+
}
73+
delay(20);
74+
}
75+
}

libraries/BluetoothSerial/src/BluetoothSerial.cpp

+35-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ static EventGroupHandle_t _spp_event_group = NULL;
5151
static boolean secondConnectionAttempt;
5252
static esp_spp_cb_t * custom_spp_callback = NULL;
5353
static BluetoothSerialDataCb custom_data_callback = NULL;
54+
static esp_bd_addr_t current_bd_addr;
55+
static ConfirmRequestCb confirm_request_callback = NULL;
56+
static AuthCompleteCb auth_complete_callback = NULL;
5457

5558
#define INQ_LEN 0x10
5659
#define INQ_NUM_RSPS 20
@@ -398,8 +401,14 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
398401
case ESP_BT_GAP_AUTH_CMPL_EVT:
399402
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
400403
log_v("authentication success: %s", param->auth_cmpl.device_name);
404+
if (auth_complete_callback) {
405+
auth_complete_callback(true);
406+
}
401407
} else {
402408
log_e("authentication failed, status:%d", param->auth_cmpl.stat);
409+
if (auth_complete_callback) {
410+
auth_complete_callback(false);
411+
}
403412
}
404413
break;
405414

@@ -421,7 +430,13 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
421430

422431
case ESP_BT_GAP_CFM_REQ_EVT:
423432
log_i("ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
424-
esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
433+
if (confirm_request_callback) {
434+
memcpy(current_bd_addr, param->cfm_req.bda, sizeof(esp_bd_addr_t));
435+
confirm_request_callback(param->cfm_req.num_val);
436+
}
437+
else {
438+
esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
439+
}
425440
break;
426441

427442
case ESP_BT_GAP_KEY_NOTIF_EVT:
@@ -500,7 +515,9 @@ static bool _init_bt(const char *deviceName)
500515
}
501516
}
502517

503-
if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
518+
// Why only master need this? Slave need this during pairing as well
519+
// if (_isMaster && esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
520+
if (esp_bt_gap_register_callback(esp_bt_gap_cb) != ESP_OK) {
504521
log_e("gap register failed");
505522
return false;
506523
}
@@ -672,6 +689,22 @@ void BluetoothSerial::end()
672689
_stop_bt();
673690
}
674691

692+
void BluetoothSerial::onConfirmRequest(ConfirmRequestCb cb)
693+
{
694+
confirm_request_callback = cb;
695+
}
696+
697+
void BluetoothSerial::onAuthComplete(AuthCompleteCb cb)
698+
{
699+
auth_complete_callback = cb;
700+
}
701+
702+
void BluetoothSerial::confirmReply(boolean confirm)
703+
{
704+
esp_bt_gap_ssp_confirm_reply(current_bd_addr, confirm);
705+
}
706+
707+
675708
esp_err_t BluetoothSerial::register_callback(esp_spp_cb_t * callback)
676709
{
677710
custom_spp_callback = callback;

libraries/BluetoothSerial/src/BluetoothSerial.h

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <functional>
2626

2727
typedef std::function<void(const uint8_t *buffer, size_t size)> BluetoothSerialDataCb;
28+
typedef std::function<void(uint32_t num_val)> ConfirmRequestCb;
29+
typedef std::function<void(boolean success)> AuthCompleteCb;
2830

2931
class BluetoothSerial: public Stream
3032
{
@@ -44,6 +46,10 @@ class BluetoothSerial: public Stream
4446
void end(void);
4547
void onData(BluetoothSerialDataCb cb);
4648
esp_err_t register_callback(esp_spp_cb_t * callback);
49+
50+
void onConfirmRequest(ConfirmRequestCb cb);
51+
void onAuthComplete(AuthCompleteCb cb);
52+
void confirmReply(boolean confirm);
4753

4854
void enableSSP();
4955
bool setPin(const char *pin);

0 commit comments

Comments
 (0)