Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .github/workflows/platformio-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
- "SerialToSerialBLE"
# - "SerialToSerialBLE_TransparentUART"
- "SerialToSerialBLE_TransparentUART-NimBLE"
# - "SerialToSerialBLE_Secure"
boards:
- [ esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1 ]
nimble:
Expand All @@ -47,6 +48,11 @@ jobs:
os: ubuntu-latest
boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1]

- example: "SerialToSerialBLE_Secure"
nimble: false
os: ubuntu-latest
boards: [esp32dev, esp32-s3-devkitc-1, esp32-c3-devkitm-1]

steps:
- uses: actions/checkout@v4

Expand Down
86 changes: 86 additions & 0 deletions examples/SerialToSerialBLE_Secure/SerialToSerialBLE_Secure.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <BLESerial.h>

// FOR ETL: Uncomment the following lines
// #include <Embedded_Template_Library.h>
// #include <etl/queue.h>
// #include <etl/circular_buffer.h>

BLESerial SerialBLE;
// If you are using an older version of Arduino IDE or C++ compiler, you may need to use
// an empty template argument (<>), as Class Template Argument Deduction (CTAD) is not
// supported in C++ versions older than C++17. For more details, see:
// https://www.cppreference.com/w/cpp/language/ctad.html
//
// Uncomment the line below if you are using an older version of Arduino IDE/C++ compiler
// BLESerial<> SerialBLE;

// FOR ETL: Uncomment one of the following lines
// BLESerial<etl::queue<uint8_t, 255, etl::memory_model::MEMORY_MODEL_SMALL>> SerialBLE;
// OR
// BLESerial<etl::circular_buffer<uint8_t, 255>> SerialBLE;

class AppSecurityCallbacks : public BLESecurityCallbacks {
public:
AppSecurityCallbacks() {
this->passKey = random(111111, 999999);
}

uint32_t onPassKeyRequest(){
ESP_LOGI(LOG_TAG, "PassKeyRequest");
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.

// Generate a random passkey
this->passKey = random(111111, 999999);

return this->passKey;
}

void onPassKeyNotify(uint32_t pass_key){
ESP_LOGI(LOG_TAG, "The passkey Notify number: %d", pass_key);
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
}

bool onConfirmPIN(uint32_t pass_key){
ESP_LOGI(LOG_TAG, "The passkey YES/NO number: %d", pass_key);
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
vTaskDelay(5000);
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hardcoded 5000ms delay (5 seconds) is a magic number. Consider defining it as a named constant or adding a comment explaining why this specific delay is needed for the confirmation process.

Suggested change
vTaskDelay(5000);
vTaskDelay(CONFIRMATION_DELAY_MS);

Copilot uses AI. Check for mistakes.
return true;
}

bool onSecurityRequest(){
ESP_LOGI(LOG_TAG, "SecurityRequest");
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
return true;
}

void onAuthenticationComplete(esp_ble_auth_cmpl_t cmpl){
ESP_LOGI(LOG_TAG, "Starting BLE work!");
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG_TAG is used but not defined. This will cause a compilation error. Define LOG_TAG as a const char* or use a string literal directly.

Copilot uses AI. Check for mistakes.
}

private:
uint32_t passKey;
};

void setup() {
BLEDevice::init("ESP32-BLE-Slave");
BLEDevice::setEncryptionLevel(ESP_BLE_SEC_ENCRYPT);
BLEDevice::setSecurityCallbacks(new AppSecurityCallbacks());

BLEServer* pServer = BLEDevice::createServer();

BLESecurity *pSecurity = new BLESecurity();
Copy link

Copilot AI Jul 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory allocated for BLESecurity is never freed, causing a memory leak. Consider using a smart pointer or ensuring proper cleanup, or making it a stack variable if the lifetime permits.

Copilot uses AI. Check for mistakes.
pSecurity->setKeySize();
pSecurity->setAuthenticationMode(ESP_LE_AUTH_REQ_SC_ONLY);
pSecurity->setCapability(ESP_IO_CAP_IO);
pSecurity->setInitEncryptionKey(ESP_BLE_ENC_KEY_MASK | ESP_BLE_ID_KEY_MASK);

Serial.begin(9600);
SerialBLE.begin(pServer);
}

void loop() {
if (Serial.available()) {
SerialBLE.write(Serial.read());
SerialBLE.flush();
}

if (SerialBLE.available()) {
Serial.write(SerialBLE.read());
}
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "Serial_BLE",
"version": "1.2.0",
"version": "1.2.1",
"description": "Customizable Arduino and ESP32 BLE Serial library, compliant with Nordic UART Service and others.",
"keywords": [
"serial",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Serial_BLE
version=1.2.0
version=1.2.1
author=Leonid Meleshin <[email protected]>
maintainer=Leonid Meleshin <[email protected]>
sentence=Customizable BLE Serial (UART) library.
Expand Down