Skip to content

Commit fff763c

Browse files
committed
Clarified useage of SPP example
1 parent d2d6331 commit fff763c

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

libraries/BluetoothSerial/examples/SerialToSerialBT_SSP/SerialToSerialBT_SSP.ino

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,26 @@
55
// and also demonstrate that SerialBT have the same functionalities of a normal Serial
66
// SSP - Simple Secure Pairing - The device (ESP32) will display random number and the user is responsible of comparing it to the number
77
// displayed on the other device (for example phone).
8-
// If the numbers match the user authenticates the pairing.
8+
// If the numbers match the user authenticates the pairing on both devices - on phone simply press "Pair" and in terminal for the sketch send 'Y' or 'y' to confirm.
9+
// Alternatively uncomment AUTO_PAIR to skip the terminal confirmation.
910

1011
#include "BluetoothSerial.h"
1112

13+
//#define AUTO_PAIR // Uncomment to skip the terminal confirmation.
14+
15+
// Check if BlueTooth is available
1216
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
13-
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
17+
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
1418
#endif
1519

20+
// Check Serial Port Profile
1621
#if !defined(CONFIG_BT_SPP_ENABLED)
17-
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
22+
#error Serial Port Profile for BlueTooth is not available or not enabled. It is only available for the ESP32 chip.
23+
#endif
24+
25+
// Check Simple Secure Pairing
26+
#if !defined(CONFIG_BT_SSP_ENABLED)
27+
#error Simple Secure Pairing for BlueTooth is not available or not enabled.
1828
#endif
1929

2030
const char * deviceName = "ESP32_SSP_example";
@@ -25,7 +35,11 @@ boolean confirmRequestPending = true;
2535
void BTConfirmRequestCallback(uint32_t numVal)
2636
{
2737
confirmRequestPending = true;
28-
Serial.printf("The PIN is: %lu\n", numVal);
38+
#ifndef AUTO_PAIR
39+
Serial.printf("The PIN is: %lu. If it matches number displayed on the other device write \'Y\' or \'y\':\n", numVal);
40+
#else
41+
SerialBT.confirmReply(true);
42+
#endif
2943
}
3044

3145
void BTAuthCompleteCallback(boolean success)
@@ -41,19 +55,36 @@ void BTAuthCompleteCallback(boolean success)
4155
}
4256
}
4357

58+
void serial_response(){
59+
if (Serial.available())
60+
{
61+
SerialBT.write(Serial.read());
62+
}
63+
if (SerialBT.available())
64+
{
65+
Serial.write(SerialBT.read());
66+
}
67+
delay(20);
68+
}
4469

4570
void setup()
4671
{
4772
Serial.begin(115200);
4873
SerialBT.enableSSP();
74+
SerialBT.dropCache();
4975
SerialBT.onConfirmRequest(BTConfirmRequestCallback);
5076
SerialBT.onAuthComplete(BTAuthCompleteCallback);
5177
SerialBT.begin(deviceName); //Bluetooth device name
52-
Serial.printf("The device started with name \"%s\", now you can pair it with Bluetooth!\n", deviceName);
78+
#ifndef AUTO_PAIR
79+
Serial.printf("The device started with name \"%s\", now you can pair it with Bluetooth!\nIf the numbers in terminal and on the other device match, press \"Pair\" in phone/computer and write \'Y\' or \'y\' in terminal\n", deviceName);
80+
#else
81+
Serial.printf("The device started with name \"%s\", now you can pair it with Bluetooth!\nIf the numbers in terminal and on the other device match, press \"Pair\" in phone/computer.\n", deviceName);
82+
#endif
5383
}
5484

5585
void loop()
5686
{
87+
#ifndef AUTO_PAIR
5788
if (confirmRequestPending)
5889
{
5990
if (Serial.available())
@@ -71,14 +102,9 @@ void loop()
71102
}
72103
else
73104
{
74-
if (Serial.available())
75-
{
76-
SerialBT.write(Serial.read());
77-
}
78-
if (SerialBT.available())
79-
{
80-
Serial.write(SerialBT.read());
81-
}
82-
delay(20);
105+
serial_response();
83106
}
107+
#else
108+
serial_response();
109+
#endif
84110
}

0 commit comments

Comments
 (0)