-
Notifications
You must be signed in to change notification settings - Fork 7.6k
BT serial: SSP improvements, added missing events #8453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 14 commits
bf8cfac
c1c3e2b
cec60c0
6459500
cb13745
d5a1bbc
68c4f3a
895952f
7703ebe
b5a09c4
8a7cdb8
672395f
be66f01
f419cd5
22b2e44
e20770e
2e0daf2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,79 @@ | ||
### Bluetooth Serial Library | ||
## Bluetooth Serial Library | ||
|
||
A simple Serial compatible library using ESP32 classical bluetooth (SPP) | ||
A simple Serial compatible library using ESP32 classical Bluetooth Serial Port Profile (SPP) | ||
|
||
Note: Since version 3.0.0 this library does not support legacy pairing (using fixed PIN consisting of 4 digits). | ||
|
||
### How to use it? | ||
|
||
#### How to use it? | ||
There are 3 basic use cases: phone, other ESP32 or any MCU with BT serial module | ||
|
||
- Download one bluetooth terminal app in your smartphone<br> | ||
For Android: https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal <br> | ||
For iOS: https://itunes.apple.com/us/app/hm10-bluetooth-serial-lite/id1030454675 | ||
#### Phone | ||
|
||
- Download one of the Bluetooth terminal apps to your smartphone | ||
|
||
- For [Android](https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal) | ||
- For [iOS](https://itunes.apple.com/us/app/hm10-bluetooth-serial-lite/id1030454675) | ||
|
||
- Flash an example sketch to your ESP32 | ||
|
||
- Scan and pair the device in your smartphone | ||
- Scan and pair the device to your smartphone | ||
|
||
- Open the bluetooth terminal app | ||
- Open the Bluetooth terminal app and connect | ||
|
||
- Enjoy | ||
|
||
#### ESP32 | ||
|
||
You can flash one of the ESP32 with the example [`SerialToSerialBTM`](https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino) (the Master) and another ESP32 with [`SerialToSerialBT`](https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino) (the Slave). | ||
Those examples are preset to work out-of-the-box but they should be scalable to connect multiple Slaves to the Master. | ||
Comment on lines
+26
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, this is really strange... for me, it is also not working. However, I can connect to the slave from my smartphone. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, using my smartphone I also managed to connect. There might be something weird happening in the master sketch. |
||
|
||
#### 3rd party Serial BT module | ||
|
||
Using 3rd party Serial BT module will require to study the documentation of the particular module in order to make it work, however, one side can utilize the mentioned [`SerialToSerialBTM`](https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino) (the Master) or [`SerialToSerialBT`](https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino) (the Slave). | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
### Pairing options | ||
|
||
There are two easy options and one difficult. | ||
|
||
The easy options can be used as usual. These offer pairing with and without Secure Simple Pairing (SSP). | ||
|
||
The difficult option offers legacy pairing (using fixed PIN) however this must be compiled with Arduino as an IDF component with disabled sdkconfig option `CONFIG_BT_SSP_ENABLED`. | ||
|
||
#### Without SSP | ||
|
||
This method will authenticate automatically any attempt to pair and should not be used if security is a concern! This option is used for the examples [`SerialToSerialBTM`](https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino) and [`SerialToSerialBT`](https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino). | ||
|
||
### With SSP | ||
|
||
The usage of SSP provides a secure connection. This option is demonstrated in the example `SerialToSerialBT_SSP``](https://github.com/espressif/arduino-esp32/blob/master/libraries/BluetoothSerial/examples/SerialToSerialBT_SSP/SerialToSerialBT_SSP.ino) | ||
|
||
The Secure Simple Pairing is enabled by calling method `enableSSP` which has two variants - one is backward compatible without parameter `enableSSP()` and second with parameters `enableSSP(bool inputCapability, bool outputCapability)`. | ||
Alternatively, the SSP can be disabled by `disableSSP()` | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Both options must be called before `begin()` or if it is called after `begin()` the driver needs to be restarted (call `end()` followed by `begin()`) in order to take in effect enabling or disabling the SSP. | ||
|
||
#### The parameters define the method of authentication: | ||
|
||
**inputCapability** - Defines if ESP32 device has input method (Serial terminal, keyboard or similar) | ||
|
||
**outputCapability** - Defines if ESP32 device has output method (Serial terminal, display or similar) | ||
|
||
* **inputCapability=true and outputCapability=true** | ||
* Both devices display randomly generated code and if they match the user will authenticate pairing on both devices. | ||
* This must be implemented by registering a callback via `onConfirmRequest()` and in this callback the user will input the response and call `confirmReply(true)` if the authenticated, otherwise call `confirmReply(false)` to reject the pairing. | ||
* **inputCapability=false and outputCapability=false** | ||
* Only the other device authenticates pairing without any pin. | ||
* **inputCapability=false and outputCapability=true** | ||
* Only the other device authenticates pairing without any pin. | ||
* **inputCapability=true and outputCapability=false** | ||
* The user will be required to input the passkey to the ESP32 device to authenticate. | ||
* This must be implemented by registering a callback via `onKeyRequest`()` and in this callback the entered passkey will be responded via `respondPasskey(passkey)` | ||
|
||
### Legacy Pairing (IDF component) | ||
|
||
To use Legacy pairing you will have to use [Arduino as an IDF component](https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/esp-idf_component.html) and disable option `CONFIG_BT_SSP_ENABLED`. | ||
Please refer to the documentation on how to setup Arduino as an IDF component and when you are done, run `idf.py menuconfig` navigate to `Component Config -> Bluetooth -> Bluedroid -> [ ] Secure Simple Pairing` and disable it. | ||
While in the menuconfig you will also need to change the partition scheme `Partition Table -> Partition Table -> (X) Single Factory app (large), no OTA`. | ||
After these changes save & quit menuconfig and you are ready to go: `idf.py monitor flash`. | ||
Please note that to use the PIN in smartphones and computers you need to use characters `SerialBT.setPin("1234", 4);` not a number `SerialBT.setPin(1234, 4);` . Numbers CAN be used if the other side uses them too, but phones and computers use characters. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// This example code is in the Public Domain (or CC0 licensed, at your option.) | ||
// | ||
// This example creates a bridge between Serial and Classical Bluetooth (SPP with authentication) | ||
// and also demonstrate that SerialBT have the same functionalities of a normal Serial | ||
// Legacy pairing TODO | ||
// Must be run as idf component ... todo | ||
|
||
#include "BluetoothSerial.h" | ||
|
||
// Check if BlueTooth is available | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) | ||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it | ||
#endif | ||
|
||
// Check Serial Port Profile | ||
#if !defined(CONFIG_BT_SPP_ENABLED) | ||
#error Serial Port Profile for BlueTooth is not available or not enabled. It is only available for the ESP32 chip. | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#endif | ||
|
||
// Check Simple Secure Pairing | ||
#if defined(CONFIG_BT_SSP_ENABLED) | ||
#warning Legacy Pairing is disabled (the CONFIG_BT_SSP_ENABLED is enabled) disable it in menuconfig. | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void setup(){} | ||
void loop(){} | ||
#else | ||
const char * deviceName = "ESP32_Legacy_example"; | ||
|
||
BluetoothSerial SerialBT; | ||
bool confirmRequestDone = false; | ||
|
||
void BTAuthCompleteCallback(boolean success){ | ||
if (success){ | ||
confirmRequestDone = true; | ||
Serial.println("Pairing success!!"); | ||
} | ||
else{ | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Serial.println("Pairing failed, rejected by user!!"); | ||
} | ||
} | ||
|
||
void serial_response(){ | ||
if (Serial.available()){ | ||
SerialBT.write(Serial.read()); | ||
} | ||
if (SerialBT.available()){ | ||
Serial.write(SerialBT.read()); | ||
} | ||
delay(20); | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void setup(){ | ||
Serial.begin(115200); | ||
SerialBT.onAuthComplete(BTAuthCompleteCallback); | ||
SerialBT.begin(deviceName); // Initiate Bluetooth device with name in parameter | ||
SerialBT.setPin("1234", 4); | ||
Serial.printf("The device started with name \"%s\", now you can pair it with Bluetooth!\n", deviceName); | ||
} | ||
|
||
void loop(){ | ||
if (confirmRequestDone){ | ||
serial_response(); | ||
}else{ | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
delay(1); // Feed the watchdog | ||
} | ||
} | ||
#endif | ||
PilnyTomas marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.