From 25ea953cc3d744fde55b0cd78ffe222b4af5715f Mon Sep 17 00:00:00 2001 From: liamcottle Date: Fri, 12 Sep 2025 20:23:21 +1200 Subject: [PATCH 1/2] don't mark as connected until connection secured --- src/helpers/nrf52/SerialBLEInterface.cpp | 14 ++++++++++---- src/helpers/nrf52/SerialBLEInterface.h | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/helpers/nrf52/SerialBLEInterface.cpp b/src/helpers/nrf52/SerialBLEInterface.cpp index 4bc9d10a..ad5823f1 100644 --- a/src/helpers/nrf52/SerialBLEInterface.cpp +++ b/src/helpers/nrf52/SerialBLEInterface.cpp @@ -4,10 +4,7 @@ static SerialBLEInterface* instance; void SerialBLEInterface::onConnect(uint16_t connection_handle) { BLE_DEBUG_PRINTLN("SerialBLEInterface: connected"); - if(instance){ - instance->_isDeviceConnected = true; - // no need to stop advertising on connect, as the ble stack does this automatically - } + // we now set _isDeviceConnected=true in onSecured callback instead } void SerialBLEInterface::onDisconnect(uint16_t connection_handle, uint8_t reason) { @@ -18,6 +15,14 @@ void SerialBLEInterface::onDisconnect(uint16_t connection_handle, uint8_t reason } } +void SerialBLEInterface::onSecured(uint16_t connection_handle) { + BLE_DEBUG_PRINTLN("SerialBLEInterface: onSecured"); + if(instance){ + instance->_isDeviceConnected = true; + // no need to stop advertising on connect, as the ble stack does this automatically + } +} + void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) { instance = this; @@ -36,6 +41,7 @@ void SerialBLEInterface::begin(const char* device_name, uint32_t pin_code) { Bluefruit.Periph.setConnectCallback(onConnect); Bluefruit.Periph.setDisconnectCallback(onDisconnect); + Bluefruit.Security.setSecuredCallback(onSecured); // To be consistent OTA DFU should be added first if it exists //bledfu.begin(); diff --git a/src/helpers/nrf52/SerialBLEInterface.h b/src/helpers/nrf52/SerialBLEInterface.h index 239cf6c4..bf29892d 100644 --- a/src/helpers/nrf52/SerialBLEInterface.h +++ b/src/helpers/nrf52/SerialBLEInterface.h @@ -25,6 +25,7 @@ class SerialBLEInterface : public BaseSerialInterface { void clearBuffers() { send_queue_len = 0; } static void onConnect(uint16_t connection_handle); static void onDisconnect(uint16_t connection_handle, uint8_t reason); + static void onSecured(uint16_t connection_handle); public: SerialBLEInterface() { From 400e09f318e875b63c9ddbcc180a218fa0c49acf Mon Sep 17 00:00:00 2001 From: liamcottle Date: Mon, 15 Sep 2025 13:06:35 +1200 Subject: [PATCH 2/2] revert unexpected change to ble advertising interval on nrf52 --- src/helpers/nrf52/SerialBLEInterface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/nrf52/SerialBLEInterface.cpp b/src/helpers/nrf52/SerialBLEInterface.cpp index ad5823f1..dbe6f393 100644 --- a/src/helpers/nrf52/SerialBLEInterface.cpp +++ b/src/helpers/nrf52/SerialBLEInterface.cpp @@ -86,7 +86,7 @@ void SerialBLEInterface::startAdv() { * https://developer.apple.com/library/content/qa/qa1931/_index.html */ Bluefruit.Advertising.restartOnDisconnect(false); // don't restart automatically as we handle it in onDisconnect - Bluefruit.Advertising.setInterval(32, 1600); + Bluefruit.Advertising.setInterval(32, 244); Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds