|  | 
| 1 |  | -### Bluetooth Serial Library | 
|  | 1 | +## Bluetooth Serial Library | 
| 2 | 2 | 
 | 
| 3 |  | -A simple Serial compatible library using ESP32 classical bluetooth (SPP) | 
|  | 3 | +A simple Serial compatible library using ESP32 classical Bluetooth Serial Port Profile (SPP) | 
| 4 | 4 | 
 | 
|  | 5 | +Note: Since version 3.0.0 this library does not support legacy pairing (using fixed PIN consisting of 4 digits). | 
| 5 | 6 | 
 | 
|  | 7 | +### How to use it? | 
| 6 | 8 | 
 | 
| 7 |  | -#### How to use it? | 
|  | 9 | +There are 3 basic use cases: phone, other ESP32 or any MCU with a Bluetooth serial module | 
| 8 | 10 | 
 | 
| 9 |  | -- Download one bluetooth terminal app in your smartphone<br> | 
| 10 |  | -For Android: https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal <br> | 
| 11 |  | -For iOS: https://itunes.apple.com/us/app/hm10-bluetooth-serial-lite/id1030454675 | 
|  | 11 | +#### Phone | 
|  | 12 | + | 
|  | 13 | +- Download one of the Bluetooth terminal apps to your smartphone | 
|  | 14 | + | 
|  | 15 | +    - For [Android](https://play.google.com/store/apps/details?id=de.kai_morich.serial_bluetooth_terminal) | 
|  | 16 | +    - For [iOS](https://itunes.apple.com/us/app/hm10-bluetooth-serial-lite/id1030454675) | 
| 12 | 17 | 
 | 
| 13 | 18 | - Flash an example sketch to your ESP32 | 
| 14 | 19 | 
 | 
| 15 |  | -- Scan and pair the device in your smartphone | 
|  | 20 | +- Scan and pair the device to your smartphone | 
| 16 | 21 | 
 | 
| 17 |  | -- Open the bluetooth terminal app | 
|  | 22 | +- Open the Bluetooth terminal app and connect | 
| 18 | 23 | 
 | 
| 19 | 24 | - Enjoy | 
|  | 25 | + | 
|  | 26 | +#### ESP32 | 
|  | 27 | + | 
|  | 28 | +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). | 
|  | 29 | +Those examples are preset to work out-of-the-box but they should be scalable to connect multiple Slaves to the Master. | 
|  | 30 | + | 
|  | 31 | +#### 3rd party Serial Bluetooth module | 
|  | 32 | + | 
|  | 33 | +Using a 3rd party Serial Bluetooth 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). | 
|  | 34 | + | 
|  | 35 | +### Pairing options | 
|  | 36 | + | 
|  | 37 | +There are two easy options and one difficult. | 
|  | 38 | + | 
|  | 39 | +The easy options can be used as usual. These offer pairing with and without Secure Simple Pairing (SSP). | 
|  | 40 | + | 
|  | 41 | +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`. | 
|  | 42 | + | 
|  | 43 | +#### Without SSP | 
|  | 44 | + | 
|  | 45 | +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). | 
|  | 46 | + | 
|  | 47 | +### With SSP | 
|  | 48 | + | 
|  | 49 | +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) | 
|  | 50 | + | 
|  | 51 | +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)`. Similarly, the SSP can be disabled by calling `disableSSP()`. | 
|  | 52 | + | 
|  | 53 | +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. | 
|  | 54 | + | 
|  | 55 | +#### The parameters define the method of authentication: | 
|  | 56 | + | 
|  | 57 | +**inputCapability** - Defines if ESP32 device has input method (Serial terminal, keyboard or similar) | 
|  | 58 | + | 
|  | 59 | +**outputCapability** - Defines if ESP32 device has output method (Serial terminal, display or similar) | 
|  | 60 | + | 
|  | 61 | +* **inputCapability=true and outputCapability=true** | 
|  | 62 | +    * Both devices display randomly generated code and if they match the user will authenticate pairing on both devices. | 
|  | 63 | +    * 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. | 
|  | 64 | +* **inputCapability=false and outputCapability=false** | 
|  | 65 | +    * Only the other device authenticates pairing without any pin. | 
|  | 66 | +* **inputCapability=false and outputCapability=true** | 
|  | 67 | +    * Only the other device authenticates pairing without any pin. | 
|  | 68 | +* **inputCapability=true and outputCapability=false** | 
|  | 69 | +    * The user will be required to input the passkey to the ESP32 device to authenticate. | 
|  | 70 | +    * This must be implemented by registering a callback via `onKeyRequest`()` and in this callback the entered passkey will be responded via `respondPasskey(passkey)` | 
|  | 71 | + | 
|  | 72 | +### Legacy Pairing (IDF component) | 
|  | 73 | + | 
|  | 74 | +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`. | 
|  | 75 | +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. | 
|  | 76 | +While in the menuconfig you will also need to change the partition scheme `Partition Table -> Partition Table -> (X) Single Factory app (large), no OTA`. | 
|  | 77 | +After these changes save & quit menuconfig and you are ready to go: `idf.py  monitor flash`. | 
|  | 78 | +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. | 
0 commit comments