From 489cf961b7a2d43a3ac2e903202a43323578379b Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 17 Nov 2026 20:03:17 -0600 Subject: [PATCH 1/2] feat(ble_gatt_server): add is_initialized() and document one-server-per-boot is_initialized() returns whether this server currently owns the NimBLE stack (between a successful init() and deinit()), so an application that manages one BLE personality at a time can ask "is the radio up as this server?" without tracking it in parallel. It reports server_ != nullptr, the flag init()/deinit() already maintain. The class doc now states explicitly that NimBLE owns a single GATT server (NimBLEDevice::createServer() returns that one server) and that init()/deinit() bring the whole stack up and down, so a design switching between two GATT layouts must deinit() one before init()-ing the next rather than hold two initialized instances at once. Header-only and additive. Builds the ble_gatt_server example on esp32s3. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU --- .../ble_gatt_server/include/ble_gatt_server.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/components/ble_gatt_server/include/ble_gatt_server.hpp b/components/ble_gatt_server/include/ble_gatt_server.hpp index 3bf974f7e5..b93c193300 100644 --- a/components/ble_gatt_server/include/ble_gatt_server.hpp +++ b/components/ble_gatt_server/include/ble_gatt_server.hpp @@ -27,6 +27,16 @@ namespace espp { /// It is also responsible for handling the pairing process. /// It also manages the different services that are part of the GATT server. /// +/// @note One GATT server per boot. init() calls NimBLEDevice::init() and +/// deinit() tears the whole NimBLE stack down (NimBLEDevice::deinit()), +/// and NimBLE itself owns a single GATT server (NimBLEDevice::createServer() +/// returns that one server). So a design that needs two different GATT +/// layouts — e.g. switching a peripheral between two personalities — must +/// bring up ONE BleGattServer at a time: deinit() the first (and let the +/// stack settle) before init()-ing the second, rather than holding two +/// initialized instances at once. Use is_initialized() to check whether an +/// instance currently owns the stack. +/// /// \section ble_gatt_server_ex1 BLE GATT Server Example /// \snippet ble_gatt_server_example.cpp ble gatt server example class BleGattServer : public BaseComponent { @@ -279,6 +289,12 @@ class BleGattServer : public BaseComponent { return true; } + /// Whether this server currently owns the NimBLE stack, i.e. init() has run + /// and deinit() has not. Lets an application ask "is the radio up as this + /// server?" without tracking it in parallel (see the one-server-per-boot note + /// on the class). @return true between a successful init() and deinit(). + bool is_initialized() const { return server_ != nullptr; } + /// Enable or disable the built-in Device Information and Battery services. /// @param enabled Whether init()/start_services() create and start the /// built-in Device Information (0x180A) and Battery (0x180F) services. From 1c070d777913cc8bba7cc074008de6175033ff2b Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Tue, 17 Nov 2026 22:09:41 -0600 Subject: [PATCH 2/2] docs(ble_gatt_server): correct the stale deinit() note The @note claimed deinit() "should only be called after NimBLEDevice::deinit(true) has been called", but deinit() itself calls NimBLEDevice::deinit(true). Reword it to say so and warn against calling NimBLEDevice::deinit() separately. Found while reviewing the is_initialized() change in the same doc block. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_01DFjjnq3XCTRAXENSxsCxJU --- components/ble_gatt_server/include/ble_gatt_server.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/components/ble_gatt_server/include/ble_gatt_server.hpp b/components/ble_gatt_server/include/ble_gatt_server.hpp index b93c193300..6ae54b83bc 100644 --- a/components/ble_gatt_server/include/ble_gatt_server.hpp +++ b/components/ble_gatt_server/include/ble_gatt_server.hpp @@ -318,8 +318,9 @@ class BleGattServer : public BaseComponent { /// It also invalidates any references/pointers to the server. /// @note After calling this method, any references/pointers to the server /// and any created services/characteristics will be invalid. - /// @note This method should only be called after NimBLEDevice::deinit(true) - /// has been called, since that will free the memory used by the server. + /// @note This method itself calls NimBLEDevice::deinit(true), which frees the + /// server and every service/characteristic — that is what invalidates the + /// references above. Do not call NimBLEDevice::deinit() separately. /// @note This method will also deinitialize the device info and battery /// services. void deinit() {