Skip to content

fix(ble): Add checks for BLE initialized#12271

Merged
me-no-dev merged 2 commits intoespressif:masterfrom
lucasssvaz:fix/ble_init
Jan 21, 2026
Merged

fix(ble): Add checks for BLE initialized#12271
me-no-dev merged 2 commits intoespressif:masterfrom
lucasssvaz:fix/ble_init

Conversation

@lucasssvaz
Copy link
Member

Description of Change

This pull request improves the robustness of the BLE library by ensuring that BLE operations are only performed after successful initialization. It also changes the BLEDevice::init method to return a boolean indicating success or failure, allowing users to handle initialization errors more gracefully. Most BLE-related methods now check whether initialization has occurred and log an error if not, preventing undefined behavior.

Key changes include:

Initialization Handling and API Improvements

  • Changed BLEDevice::init to return a bool indicating success or failure, instead of void. The method now returns early on failure and sets the initialized flag only on success. (libraries/BLE/src/BLEDevice.cpp, libraries/BLE/src/BLEDevice.h, libraries/BLE/examples/Server/Server.ino) [1] [2] [3] [4] [5] [6] [7] [8] [9]

Defensive Checks for Initialization

  • Added checks in all major BLE operations (e.g., createClient, createServer, getScan, getAdvertising, setPower, getPower, whiteListAdd, whiteListRemove, setMTU, getPeerIRK, startAdvertising, stopAdvertising, setCustomGapHandler, setOwnAddrType, setOwnAddr) to ensure BLE is initialized before proceeding, logging an error and returning early if not. (libraries/BLE/src/BLEDevice.cpp) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15]

Error Handling in Client Operations

  • Added null checks after client creation in getValue and setValue to prevent dereferencing a null pointer if client creation fails. (libraries/BLE/src/BLEDevice.cpp) [1] [2]

These changes make the BLE library safer and more reliable, especially in scenarios where initialization might fail or be omitted.

Test Scenarios

Tested locally

@lucasssvaz lucasssvaz requested a review from me-no-dev January 21, 2026 19:51
@lucasssvaz lucasssvaz self-assigned this Jan 21, 2026
@lucasssvaz lucasssvaz requested a review from SuGlider as a code owner January 21, 2026 19:51
@lucasssvaz lucasssvaz added the Area: BLE Issues related to BLE label Jan 21, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 21, 2026

Messages
📖 🎉 Good Job! All checks are passing!

👋 Hello lucasssvaz, we appreciate your contribution to this project!


📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more.

🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project.

Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests.

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
4. If the change is approved and passes the tests it is merged into the default branch.

Generated by 🚫 dangerJS against 400ce4f

@me-no-dev me-no-dev added the Status: Pending Merge Pull Request is ready to be merged label Jan 21, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Jan 21, 2026

Test Results

 90 files   90 suites   33m 53s ⏱️
 67 tests  67 ✅ 0 💤 0 ❌
681 runs  681 ✅ 0 💤 0 ❌

Results for commit 400ce4f.

♻️ This comment has been updated with latest results.

@me-no-dev me-no-dev merged commit 879cc37 into espressif:master Jan 21, 2026
101 of 102 checks passed
@github-actions
Copy link
Contributor

Memory usage test (comparing PR against master branch)

The table below shows the summary of memory usage change (decrease - increase) in bytes and percentage for each target.

MemoryFLASH [bytes]FLASH [%]RAM [bytes]RAM [%]
TargetDECINCDECINCDECINCDECINC
ESP320⚠️ +1120.00⚠️ +0.01000.000.00
ESP32C30⚠️ +980.00⚠️ +0.02000.000.00
ESP32C50⚠️ +320.000.00000.000.00
ESP32C60⚠️ +520.00⚠️ +0.01000.000.00
ESP32H20⚠️ +980.00⚠️ +0.01000.000.00
ESP32P40⚠️ +1400.00⚠️ +0.02000.000.00
ESP32S30⚠️ +680.00⚠️ +0.01000.000.00
Click to expand the detailed deltas report [usage change in BYTES]
TargetESP32ESP32C3ESP32C5ESP32C6ESP32H2ESP32P4ESP32S3
ExampleFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAMFLASHRAM
libraries/BLE/examples/Beacon_Scanner⚠️ +520⚠️ +400⚠️ +320⚠️ +320⚠️ +320⚠️ +820⚠️ +480
libraries/BLE/examples/Client⚠️ +600⚠️ +520--⚠️ +520⚠️ +440⚠️ +940⚠️ +680
libraries/BLE/examples/Client_Gamepad⚠️ +600⚠️ +520----⚠️ +520⚠️ +940⚠️ +680
libraries/BLE/examples/Client_Server⚠️ +880⚠️ +620----⚠️ +620⚠️ +1120⚠️ +640
libraries/BLE/examples/Client_multiconnect⚠️ +600⚠️ +520----⚠️ +520⚠️ +940--
libraries/BLE/examples/Client_secure_static_passkey⚠️ +920⚠️ +720----⚠️ +720⚠️ +1140--
libraries/BLE/examples/EddystoneTLM_Beacon⚠️ +600⚠️ +340----⚠️ +420⚠️ +720--
libraries/BLE/examples/EddystoneURL_Beacon⚠️ +640⚠️ +420----⚠️ +340⚠️ +720--
libraries/BLE/examples/Notify⚠️ +720⚠️ +400----⚠️ +480⚠️ +900--
libraries/BLE/examples/Scan⚠️ +600⚠️ +320----⚠️ +400⚠️ +820--
libraries/BLE/examples/Server⚠️ +1120⚠️ +980----⚠️ +980⚠️ +1400--
libraries/BLE/examples/Server_Gamepad⚠️ +640⚠️ +480----⚠️ +480⚠️ +900--
libraries/BLE/examples/Server_multiconnect⚠️ +680⚠️ +500----⚠️ +420⚠️ +920--
libraries/BLE/examples/Server_secure_static_passkey⚠️ +960⚠️ +700----⚠️ +700⚠️ +1140--
libraries/BLE/examples/UART⚠️ +680⚠️ +480----⚠️ +420⚠️ +920--
libraries/BLE/examples/Write⚠️ +640⚠️ +480----⚠️ +400⚠️ +900--
libraries/BLE/examples/iBeacon⚠️ +520⚠️ +480----⚠️ +400⚠️ +900--
libraries/BLE/examples/Server_secure_authorization--⚠️ +620----⚠️ +620----

@lucasssvaz lucasssvaz deleted the fix/ble_init branch January 21, 2026 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: BLE Issues related to BLE Status: Pending Merge Pull Request is ready to be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants