From 73cadce58125141d3d7c7027d3a380416935a5ed Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Thu, 9 Oct 2025 12:44:00 -0500 Subject: [PATCH] Fix BLE stateful issues (#8287) --- src/mesh/PhoneAPI.cpp | 2 +- src/nimble/NimbleBluetooth.cpp | 23 +++-------------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/mesh/PhoneAPI.cpp b/src/mesh/PhoneAPI.cpp index d2ed2d4cb..e8dc9843c 100644 --- a/src/mesh/PhoneAPI.cpp +++ b/src/mesh/PhoneAPI.cpp @@ -734,7 +734,7 @@ int PhoneAPI::onNotify(uint32_t newValue) LOG_INFO("Tell client we have new packets %u", newValue); onNowHasData(newValue); } else { - LOG_DEBUG("(Client not yet interested in packets)"); + LOG_DEBUG("Client not yet interested in packets (state=%d)", state); } return timeout ? -1 : 0; // If we timed out, MeshService should stop iterating through observers as we just removed one diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 030e00d6b..0b3009d39 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -31,11 +31,8 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread std::vector nimble_queue; std::mutex nimble_mutex; uint8_t queue_size = 0; - bool has_fromRadio = false; uint8_t fromRadioBytes[meshtastic_FromRadio_size] = {0}; size_t numBytes = 0; - bool hasChecked = false; - bool phoneWants = false; protected: virtual int32_t runOnce() override @@ -48,10 +45,7 @@ class BluetoothPhoneAPI : public PhoneAPI, public concurrency::OSThread LOG_DEBUG("Queue_size %u", queue_size); queue_size = 0; } - if (hasChecked == false && phoneWants == true) { - numBytes = getFromRadio(fromRadioBytes); - hasChecked = true; - } + // Note: phoneWants/hasChecked logic removed since onRead() handles getFromRadio() directly // the run is triggered via NimbleBluetoothToRadioCallback and NimbleBluetoothFromRadioCallback return INT32_MAX; @@ -122,15 +116,8 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks { std::lock_guard guard(bluetoothPhoneAPI->nimble_mutex); - // If we don't have fresh data, trigger a refresh - if (!bluetoothPhoneAPI->hasChecked || bluetoothPhoneAPI->numBytes == 0) { - bluetoothPhoneAPI->phoneWants = true; - bluetoothPhoneAPI->setIntervalFromNow(0); - - // Get fresh data immediately - bluetoothPhoneAPI->numBytes = bluetoothPhoneAPI->getFromRadio(bluetoothPhoneAPI->fromRadioBytes); - bluetoothPhoneAPI->hasChecked = true; - } + // Get fresh data immediately when client reads + bluetoothPhoneAPI->numBytes = bluetoothPhoneAPI->getFromRadio(bluetoothPhoneAPI->fromRadioBytes); // Set the characteristic value with whatever data we have pCharacteristic->setValue(bluetoothPhoneAPI->fromRadioBytes, bluetoothPhoneAPI->numBytes); @@ -138,8 +125,6 @@ class NimbleBluetoothFromRadioCallback : public NimBLECharacteristicCallbacks if (bluetoothPhoneAPI->numBytes != 0) // if we did send something, queue it up right away to reload bluetoothPhoneAPI->setIntervalFromNow(0); bluetoothPhoneAPI->numBytes = 0; - bluetoothPhoneAPI->hasChecked = false; - bluetoothPhoneAPI->phoneWants = false; } }; @@ -248,8 +233,6 @@ class NimbleBluetoothServerCallback : public NimBLEServerCallbacks if (bluetoothPhoneAPI) { std::lock_guard guard(bluetoothPhoneAPI->nimble_mutex); bluetoothPhoneAPI->close(); - bluetoothPhoneAPI->hasChecked = false; - bluetoothPhoneAPI->phoneWants = false; bluetoothPhoneAPI->numBytes = 0; bluetoothPhoneAPI->queue_size = 0; }