Skip to content

Commit 5898a3e

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

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

.github/workflows/platformio-ci.yml

Lines changed: 7 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 ]
@@ -63,8 +64,11 @@ jobs:
6364
pio ci --lib="." --board=${{ join(matrix.boards, ' --board=') }} ${{ matrix.nimble && '--project-option="lib_deps=h2zero/NimBLE-Arduino@^1.4.0"' || '' }}
6465
env:
6566
PLATFORMIO_CI_SRC: "./examples/${{ matrix.example }}/*.ino"
67+
PLATFORMIO_BUILD_UNFLAGS: |
68+
-std=gnu++11
6669
PLATFORMIO_BUILD_FLAGS: |
70+
-std=gnu++17
6771
-D BLESERIAL_USE_NIMBLE=${{ matrix.nimble }}
68-
-Wall
69-
-Wextra
70-
-Wpedantic
72+
# -Wall
73+
# -Wextra
74+
# -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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 (<=std=c++17).
13+
// https://www.cppreference.com/w/cpp/language/ctad.html
14+
//
15+
// Uncomment the line below if you are using an older version of Arduino IDE
16+
// BLESerial<> SerialBLE;
17+
18+
// FOR ETL: Uncomment one of the following lines
19+
// BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
20+
// OR
21+
// BLESerial<etl::circular_buffer<uint8_t, 255>> SerialBLE;
22+
23+
uint32_t passKey;
24+
25+
class AppSecurityCallbacks : public BLESecurityCallbacks {
26+
public:
27+
AppSecurityCallbacks() {
28+
this->passKey = random(111111, 999999);
29+
}
30+
31+
uint32_t onPassKeyRequest(){
32+
ESP_LOGI(LOG_TAG, "PassKeyRequest");
33+
34+
// Generate a random passkey
35+
this->passKey = random(111111, 999999);
36+
37+
return this->passKey;
38+
}
39+
40+
void onPassKeyNotify(uint32_t pass_key){
41+
ESP_LOGI(LOG_TAG, "The passkey Notify number: %d", pass_key);
42+
}
43+
44+
bool onConfirmPIN(uint32_t pass_key){
45+
ESP_LOGI(LOG_TAG, "The passkey YES/NO number: %d", pass_key);
46+
vTaskDelay(5000);
47+
return true;
48+
}
49+
50+
bool onSecurityRequest(){
51+
ESP_LOGI(LOG_TAG, "SecurityRequest");
52+
return true;
53+
}
54+
55+
void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
56+
ESP_LOGI(LOG_TAG, "Starting BLE work!");
57+
}
58+
59+
private:
60+
uint32_t passKey;
61+
};
62+
63+
void setup() {
64+
BLEDevice::init(device_name);
65+
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
66+
BLEDevice::setSecurityCallbacks(new AppSecurityCallbacks());
67+
68+
BLEServer* pServer = BLEDevice::createServer();
69+
70+
BLESecurity *pSecurity = new BLESecurity();
71+
pSecurity->setKeySize();
72+
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
73+
pSecurity->setCapability(ESP_IO_CAP_IO);
74+
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);
75+
76+
Serial.begin(9600);
77+
SerialBLE.begin(pServer);
78+
}
79+
80+
void loop() {
81+
if (Serial.available()) {
82+
SerialBLE.write(Serial.read());
83+
SerialBLE.flush();
84+
}
85+
86+
if (SerialBLE.available()) {
87+
Serial.write(SerialBLE.read());
88+
}
89+
}

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)