Skip to content

Commit e3ae3d3

Browse files
committed
📝 Add BLESecurity example
1 parent 0bea04f commit e3ae3d3

File tree

4 files changed

+97
-7
lines changed

4 files changed

+97
-7
lines changed

.github/workflows/platformio-ci.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
os: [ ubuntu-latest ]
2222
example:
2323
- "SerialToSerialBLE"
24+
- "SerialToSerialBLE_Secure"
2425
boards:
2526
- [ esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1 ]
2627
nimble: [ false, true ]
@@ -65,6 +66,6 @@ jobs:
6566
PLATFORMIO_CI_SRC: "./examples/${{ matrix.example }}/*.ino"
6667
PLATFORMIO_BUILD_FLAGS: |
6768
-D BLESERIAL_USE_NIMBLE=${{ matrix.nimble }}
68-
-Wall
69-
-Wextra
70-
-Wpedantic
69+
# -Wall
70+
# -Wextra
71+
# -Wpedantic

examples/SerialToSerialBLE/SerialToSerialBLE.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
String device_name = "ESP32-BLE-Slave";
99

10-
// Mind the empty template argument (<>), it is required for
11-
// the code to compile with the current Arduino C++ compiler version
12-
BLESerial<> SerialBLE;
10+
BLESerial SerialBLE;
11+
// If you are using older version of Arduino IDE, you may need to use
12+
// empty template argument (<>), due to the old C++ compiler version.
13+
// Uncomment the line below if you are using an older version of Arduino IDE
14+
// BLESerial<> SerialBLE;
1315

1416
// FOR ETL: Uncomment one of the following lines
1517
// BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <BLESerial.h>
2+
3+
// FOR ETL: Uncomment the following lines
4+
// #include <Embedded_Template_Library.h>
5+
// #include <etl/queue.h>
6+
// #include <etl/circular_buffer.h>
7+
8+
String device_name = "ESP32-BLE-Slave";
9+
10+
BLESerial SerialBLE;
11+
// If you are using older version of Arduino IDE, you may need to use
12+
// empty template argument (<>), due to the old C++ compiler version.
13+
// Uncomment the line below if you are using an older version of Arduino IDE
14+
// BLESerial<> SerialBLE;
15+
16+
// FOR ETL: Uncomment one of the following lines
17+
// BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
18+
// OR
19+
// BLESerial<etl::circular_buffer<uint8_t, 255>> SerialBLE;
20+
21+
uint32_t passKey;
22+
23+
class AppSecurityCallbacks : public BLESecurityCallbacks {
24+
public:
25+
AppSecurityCallbacks() {
26+
this->passKey = random(111111, 999999);
27+
}
28+
29+
uint32_t onPassKeyRequest(){
30+
ESP_LOGI(LOG_TAG, "PassKeyRequest");
31+
32+
// Generate a random passkey
33+
this->passKey = random(111111, 999999);
34+
35+
return this->passKey;
36+
}
37+
38+
void onPassKeyNotify(uint32_t pass_key){
39+
ESP_LOGI(LOG_TAG, "The passkey Notify number: %d", pass_key);
40+
}
41+
42+
bool onConfirmPIN(uint32_t pass_key){
43+
ESP_LOGI(LOG_TAG, "The passkey YES/NO number: %d", pass_key);
44+
vTaskDelay(5000);
45+
return true;
46+
}
47+
48+
bool onSecurityRequest(){
49+
ESP_LOGI(LOG_TAG, "SecurityRequest");
50+
return true;
51+
}
52+
53+
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
54+
ESP_LOGI(LOG_TAG, "Starting BLE work!");
55+
}
56+
57+
private:
58+
uint32_t passKey;
59+
};
60+
61+
void setup() {
62+
BLEDevice::init(device_name);
63+
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
64+
BLEDevice::setSecurityCallbacks(new AppSecurityCallbacks());
65+
66+
BLEServer* pServer = BLEDevice::createServer();
67+
68+
BLESecurity *pSecurity = new BLESecurity();
69+
pSecurity->setKeySize();
70+
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
71+
pSecurity->setCapability(ESP_IO_CAP_IO);
72+
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
73+
74+
Serial.begin(9600);
75+
SerialBLE.begin(pServer);
76+
}
77+
78+
void loop() {
79+
if (Serial.available()) {
80+
SerialBLE.write(Serial.read());
81+
SerialBLE.flush();
82+
}
83+
84+
if (SerialBLE.available()) {
85+
Serial.write(SerialBLE.read());
86+
}
87+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
"srcDir": "src"
4747
},
4848
"dependencies": {
49-
"ESP32 BLE Arduino": "^2.0.0"
49+
"BLE": "^3.0.0"
5050
}
5151
}

0 commit comments

Comments
 (0)