Skip to content

Commit 74c43ac

Browse files
committed
Update example for 3.0.9 - demonstrate pairing with PIN
1 parent 6a1c825 commit 74c43ac

File tree

4 files changed

+72
-42
lines changed

4 files changed

+72
-42
lines changed

examples/ESP32_BluetoothSerial/ESP32_BluetoothSerial.ino

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
CONFIG_BTDM_CTRL_MODE_BLE_ONLY=n
4949
CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n
5050
(SDP is disabled in the standard libbt.a)
51+
It also corrects the UUID byte-order reversal in add_raw_sdp
5152
5253
Tried and tested on: Arduino esp32 v3.0.7 (IDF 5.1)
5354
@@ -106,7 +107,7 @@ const char *serialNumber = "123456";
106107
const char *firmwareVersion = "1.0.0";
107108
const char *hardwareVersion = "1.0.0";
108109
const char *EAProtocol = "com.sparkfun.rtk";
109-
const char *BTTransportName = "com.sparkfun.bt";
110+
const char *BTTransportName = "Bluetooth";
110111
const char *LIComponentName = "com.sparkfun.li";
111112
const char *productPlanUID = "0123456789ABCDEF"; // This comes from the MFi Portal, when you register the product with Apple
112113

@@ -120,7 +121,10 @@ BluetoothSerial SerialBT;
120121

121122
const char *sdp_service_name = "iAP2";
122123

123-
static const uint8_t UUID_IAP2[] = {0x00, 0x00, 0x00, 0x00, 0xDE, 0xCA, 0xFA, 0xDE, 0xDE, 0xCA, 0xDE, 0xAF, 0xDE, 0xCA, 0xCA, 0xFF};
124+
// With the byte-order correction in libbt.a btc_sdp.c add_raw_sdp() (ARRAY_TO_BE_STREAM_REVERSE replaces ARRAY_TO_BE_STREAM),
125+
// we need to provide the UUID as little-endian:
126+
// UUID : Little-Endian
127+
static const uint8_t UUID_IAP2[] = {0xFF, 0xCA, 0xCA, 0xDE, 0xAF, 0xDE, 0xCA, 0xDE, 0xDE, 0xFA, 0xCA, 0xDE, 0x00, 0x00, 0x00, 0x00};
124128

125129
uint8_t btMacAddress[6];
126130

@@ -146,7 +150,7 @@ void setup()
146150
// ==============================================================================================================
147151
// Setup Bluetooth
148152

149-
SerialBT.enableSSP(false, false); //Enable secure pairing, authenticate without displaying anything
153+
SerialBT.enableSSP(false, true); //Enable secure pairing with PIN
150154

151155
SerialBT.begin(accessoryName, true, true); //Bluetooth device name, start in master mode, disable BLE
152156

@@ -157,15 +161,20 @@ void setup()
157161
for (uint8_t i = 0; i < 6; i++)
158162
Serial.printf("%02X ",btMacAddress[i]);
159163
Serial.println();
164+
165+
// Comment the next two lines if you do not want or need to delete the previous pairings.
166+
// (But, deleting them is essential if you have changed the secure pairing mode)
167+
Serial.println("Deleting all previous bonded devices.");
168+
SerialBT.deleteAllBondedDevices(); // Must be called after begin
160169

161170
esp_sdp_init();
162171

163172
esp_bluetooth_sdp_hdr_overlay_t record = {(esp_bluetooth_sdp_types_t)0};
164173
record.type = ESP_SDP_TYPE_RAW;
165174
record.uuid.len = sizeof(UUID_IAP2);
166175
memcpy(record.uuid.uuid.uuid128, UUID_IAP2, sizeof(UUID_IAP2));
167-
record.service_name_length = strlen(sdp_service_name) + 1;
168-
record.service_name = (char *)sdp_service_name;
176+
record.service_name_length = strlen(accessoryName) + 1;
177+
record.service_name = (char *)accessoryName;
169178
esp_sdp_create_record((esp_bluetooth_sdp_record_t *)&record);
170179

171180
// ==============================================================================================================
@@ -268,10 +277,13 @@ void loop()
268277
{
269278
Serial.println("Apple Device found, connecting...");
270279

280+
delay(2);
281+
271282
SerialBT.connect(SerialBT.aclGetAddress(), 1); //Connect on channel 1
272283

273284
if (SerialBT.connected())
274285
{
286+
Serial.println("Sending handshake...");
275287
appleAccessory.startHandshake(&SerialBT);
276288
}
277289
}
-84 Bytes
Binary file not shown.

examples/ESP32_BluetoothSerial/src/BluetoothSerial/BluetoothSerial.cpp

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@
4040

4141
const char *_spp_server_name = "ESP32SPP";
4242

43-
// #define RX_QUEUE_SIZE 512
44-
// #define TX_QUEUE_SIZE 32
45-
#define RX_QUEUE_SIZE 512 * 4
46-
#define TX_QUEUE_SIZE 512 * 4
43+
// #define RX_QUEUE_SIZE 512
44+
// #define TX_QUEUE_SIZE 32
4745
#define SPP_TX_QUEUE_TIMEOUT 1000
4846
#define SPP_TX_DONE_TIMEOUT 1000
4947
#define SPP_CONGESTED_TIMEOUT 1000
@@ -114,8 +112,7 @@ typedef struct
114112
uint8_t data[];
115113
} spp_packet_t;
116114

117-
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
118-
static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
115+
char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
119116
{
120117
if (bda == NULL || str == NULL || size < 18)
121118
{
@@ -126,7 +123,6 @@ static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
126123
snprintf(str, size, "%02x:%02x:%02x:%02x:%02x:%02x", p[0], p[1], p[2], p[3], p[4], p[5]);
127124
return str;
128125
}
129-
#endif
130126

131127
static bool get_name_from_eir(uint8_t *eir, char *bdname, uint8_t *bdname_len)
132128
{
@@ -271,7 +267,7 @@ static void _spp_tx_task(void *arg)
271267
}
272268
else
273269
{
274-
log_e("Something went horribly wrong");
270+
log_e("BluetoothSerial _spp_tx_task: something went horribly wrong");
275271
}
276272
}
277273
vTaskDelete(NULL);
@@ -382,6 +378,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
382378
{
383379
log_e("ESP_SPP_CLOSE_EVT failed!, status:%d", param->close.status);
384380
}
381+
385382
break;
386383

387384
case ESP_SPP_START_EVT: // Enum 28 - When SPP server started
@@ -414,7 +411,7 @@ static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
414411
{
415412
if (xQueueSend(_spp_rx_queue, param->data_ind.data + i, (TickType_t)0) != pdTRUE)
416413
{
417-
log_e("RX Full! Discarding %u bytes", param->data_ind.len - i);
414+
log_e("BluetoothSerial RX Full! Discarding %u bytes\r\n", param->data_ind.len - i);
418415
break;
419416
}
420417
}
@@ -563,9 +560,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
563560
case ESP_BT_GAP_DEV_PROP_EIR: // Enum 4 - Extended Inquiry Response, value type is uint8_t []
564561
if (get_name_from_eir((uint8_t *)param->disc_res.prop[i].val, peer_bdname, &peer_bdname_len))
565562
{
566-
567563
log_i("ESP_BT_GAP_DISC_RES_EVT : EIR : %s : %d", peer_bdname, peer_bdname_len);
568-
569564
if (strlen(_remote_name) == peer_bdname_len &&
570565
strncmp(peer_bdname, _remote_name, peer_bdname_len) == 0)
571566
{
@@ -628,15 +623,15 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
628623
case ESP_BT_GAP_AUTH_CMPL_EVT: // Enum 4 - Authentication complete event
629624
if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS)
630625
{
631-
log_v("authentication success: %s", param->auth_cmpl.device_name);
626+
log_v("ESP_BT_GAP_AUTH_CMPL_EVT authentication success: %s", param->auth_cmpl.device_name);
632627
if (auth_complete_callback)
633628
{
634629
auth_complete_callback(true);
635630
}
636631
}
637632
else
638633
{
639-
log_e("authentication failed, status:%d", param->auth_cmpl.stat);
634+
log_e("ESP_BT_GAP_AUTH_CMPL_EVT authentication failed, status:%d", param->auth_cmpl.stat);
640635
if (auth_complete_callback)
641636
{
642637
auth_complete_callback(false);
@@ -657,8 +652,12 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
657652
}
658653
break;
659654
#ifdef CONFIG_BT_SSP_ENABLED
660-
case ESP_BT_GAP_CFM_REQ_EVT: // Enum 6 - Security Simple Pairing User Confirmation request.
661-
log_i("ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
655+
case ESP_BT_GAP_CFM_REQ_EVT: // Enum 6 - Security Simple Pairing User Confirmation request.
656+
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
657+
char bda_str[18];
658+
log_i("ESP_BT_GAP_CFM_REQ_EVT from %s. Please compare the numeric value: %d",
659+
bda2str(param->cfm_req.bda, bda_str, 18), param->cfm_req.num_val);
660+
#endif
662661
if (confirm_request_callback)
663662
{
664663
memcpy(current_bd_addr, param->cfm_req.bda, sizeof(esp_bd_addr_t));
@@ -667,15 +666,17 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
667666
else
668667
{
669668
// log_w("ESP_BT_GAP_CFM_REQ_EVT: confirm_request_callback does not exist - refusing pairing");
670-
// esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, false);
671-
log_w("ESP_BT_GAP_CFM_REQ_EVT: confirming replay");
669+
// esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, false);
670+
log_w("ESP_BT_GAP_CFM_REQ_EVT: confirming reply");
672671
esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
673672
}
674673
break;
675674
#endif
675+
676676
case ESP_BT_GAP_KEY_NOTIF_EVT: // Enum 7 - Security Simple Pairing Passkey Notification
677677
log_i("ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
678678
break;
679+
679680
#ifdef CONFIG_BT_SSP_ENABLED
680681
case ESP_BT_GAP_KEY_REQ_EVT: // Enum 8 - Security Simple Pairing Passkey request
681682
log_i("ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
@@ -691,10 +692,10 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
691692
}
692693
break;
693694
#endif
695+
694696
case ESP_BT_GAP_READ_RSSI_DELTA_EVT: // Enum 9 - Read rssi event
695697
log_i("ESP_BT_GAP_READ_RSSI_DELTA_EVT Read rssi event");
696698
break;
697-
698699
case ESP_BT_GAP_CONFIG_EIR_DATA_EVT: // Enum 10 - Config EIR data event
699700
log_i("ESP_BT_GAP_CONFIG_EIR_DATA_EVT: stat:%d num:%d", param->config_eir_data.stat,
700701
param->config_eir_data.eir_type_num);
@@ -731,8 +732,11 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
731732

732733
case ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT: // Enum 16 - ACL connection complete status event
733734
{
735+
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
734736
char bda_str[18];
735-
log_i("ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT ACL connection from: %s", bda2str(param->acl_conn_cmpl_stat.bda, bda_str, 18));
737+
log_i("ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT ACL connection from: %s",
738+
bda2str(param->acl_conn_cmpl_stat.bda, bda_str, 18));
739+
#endif
736740

737741
memcpy(_aclAddress, param->acl_conn_cmpl_stat.bda, ESP_BD_ADDR_LEN);
738742

@@ -757,7 +761,7 @@ static void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa
757761
}
758762
}
759763

760-
static bool _init_bt(const char *deviceName, bt_mode mode)
764+
static bool _init_bt(const char *deviceName, bt_mode mode, uint16_t rxQueueSize, uint16_t txQueueSize)
761765
{
762766
if (!_bt_event_group)
763767
{
@@ -784,7 +788,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
784788
}
785789
if (_spp_rx_queue == NULL)
786790
{
787-
_spp_rx_queue = xQueueCreate(RX_QUEUE_SIZE, sizeof(uint8_t)); // initialize the queue
791+
//_spp_rx_queue = xQueueCreate(RX_QUEUE_SIZE, sizeof(uint8_t)); //initialize the queue
792+
_spp_rx_queue = xQueueCreate(rxQueueSize, sizeof(uint8_t)); // initialize the queue
788793
if (_spp_rx_queue == NULL)
789794
{
790795
log_e("RX Queue Create Failed");
@@ -793,7 +798,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
793798
}
794799
if (_spp_tx_queue == NULL)
795800
{
796-
_spp_tx_queue = xQueueCreate(TX_QUEUE_SIZE, sizeof(spp_packet_t *)); // initialize the queue
801+
//_spp_tx_queue = xQueueCreate(TX_QUEUE_SIZE, sizeof(spp_packet_t *)); //initialize the queue
802+
_spp_tx_queue = xQueueCreate(txQueueSize, sizeof(spp_packet_t *)); // initialize the queue
797803
if (_spp_tx_queue == NULL)
798804
{
799805
log_e("TX Queue Create Failed");
@@ -830,14 +836,14 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
830836
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
831837
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED)
832838
{
833-
//#ifdef CONFIG_BT_SSP_ENABLED
839+
// #ifdef CONFIG_BT_SSP_ENABLED
834840
#if false
835841
// Start with SSP
836842
esp_bluedroid_config_t bluedroid_cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();
837843
if (_enableSSP == false)
838844
bluedroid_cfg.ssp_en = false;
839845

840-
if (esp_bluedroid_init_with_cfg(&bluedroid_cfg))
846+
if (esp_bluedroid_init_with_cfg(&bluedroid_cfg)) // *** Not supported by IDF5.1 ***
841847
{
842848
log_e("initialize bluedroid failed");
843849
return false;
@@ -882,7 +888,8 @@ static bool _init_bt(const char *deviceName, bt_mode mode)
882888

883889
log_i("device name set");
884890
esp_bt_dev_set_device_name(deviceName);
885-
//esp_bt_gap_set_device_name(deviceName);
891+
// Note: in later cores, this changes to:
892+
// esp_bt_gap_set_device_name(deviceName);
886893

887894
#ifdef CONFIG_BT_SSP_ENABLED
888895
if (_enableSSP)
@@ -989,10 +996,12 @@ static bool waitForConnect(int timeout)
989996
}
990997
else if ((rc & SPP_CLOSED) != 0)
991998
{
992-
log_d("connection closed!");
999+
if (timeout > 0)
1000+
log_d("connection closed!");
9931001
return false;
9941002
}
995-
log_d("timeout");
1003+
if (timeout > 0)
1004+
log_d("timeout");
9961005
return false;
9971006
}
9981007

@@ -1029,14 +1038,16 @@ BluetoothSerial::~BluetoothSerial(void)
10291038
* @param isMaster set to true if you want to connect to an other device
10301039
* @param disableBLE if BLE is not used, its ram can be freed to get +10kB free ram
10311040
*/
1032-
bool BluetoothSerial::begin(String localName, bool isMaster, bool disableBLE)
1041+
// bool BluetoothSerial::begin(String localName, bool isMaster, bool disableBLE) {
1042+
bool BluetoothSerial::begin(String localName, bool isMaster, bool disableBLE, uint16_t rxQueueSize,
1043+
uint16_t txQueueSize)
10331044
{
10341045
_isMaster = isMaster;
10351046
if (localName.length())
10361047
{
10371048
local_name = localName;
10381049
}
1039-
return _init_bt(local_name.c_str(), disableBLE ? BT_MODE_CLASSIC_BT : BT_MODE_BTDM);
1050+
return _init_bt(local_name.c_str(), disableBLE ? BT_MODE_CLASSIC_BT : BT_MODE_BTDM, rxQueueSize, txQueueSize);
10401051
}
10411052

10421053
int BluetoothSerial::available(void)
@@ -1103,7 +1114,7 @@ void BluetoothSerial::flush()
11031114
{
11041115
while (uxQueueMessagesWaiting(_spp_tx_queue) > 0)
11051116
{
1106-
delay(2);
1117+
delay(2); // https://github.com/espressif/arduino-esp32/pull/9905
11071118
}
11081119
}
11091120
}
@@ -1145,6 +1156,11 @@ void BluetoothSerial::onAuthComplete(AuthCompleteCb cb)
11451156

11461157
void BluetoothSerial::confirmReply(boolean confirm)
11471158
{
1159+
#if (ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_INFO)
1160+
char bda_str[18];
1161+
log_i("BluetoothSerial::confirmReply : %s : %s",
1162+
bda2str(current_bd_addr, bda_str, 18), confirm ? "true" : "false");
1163+
#endif
11481164
esp_bt_gap_ssp_confirm_reply(current_bd_addr, confirm);
11491165
}
11501166

@@ -1703,19 +1719,21 @@ void BluetoothSerial::deleteAllBondedDevices()
17031719
}
17041720
}
17051721

1722+
// aclConnected() is a one-shot. __aclConnected is cleared when returning true
17061723
bool BluetoothSerial::aclConnected()
17071724
{
1708-
if (_aclConnected == true)
1725+
if (_aclConnected)
17091726
{
17101727
_aclConnected = false;
1711-
return (true);
1728+
return true;
17121729
}
1713-
return (false);
1730+
1731+
return false;
17141732
}
17151733

1716-
uint8_t* BluetoothSerial::aclGetAddress()
1734+
uint8_t *BluetoothSerial::aclGetAddress()
17171735
{
1718-
return (_aclAddress);
1736+
return (&_aclAddress[0]);
17191737
}
17201738

17211739
#endif // defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)

examples/ESP32_BluetoothSerial/src/BluetoothSerial/BluetoothSerial.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class BluetoothSerial : public Stream {
3939
BluetoothSerial(void);
4040
~BluetoothSerial(void);
4141

42-
bool begin(String localName = String(), bool isMaster = false, bool disableBLE = false);
42+
bool begin(String localName = String(), bool isMaster = false, bool disableBLE = false, uint16_t rxQueueSize = 512 * 2, uint16_t txQueueSize = 512);
4343
bool begin(unsigned long baud) { //compatibility
4444
return begin();
4545
}
@@ -114,7 +114,7 @@ class BluetoothSerial : public Stream {
114114
bool deleteBondedDevice(uint8_t *remoteAddress);
115115
void deleteAllBondedDevices();
116116

117-
bool aclConnected();
117+
bool aclConnected(); // One-shot
118118
uint8_t *aclGetAddress();
119119

120120
private:

0 commit comments

Comments
 (0)