From a8d1a90e16ddc1defa29e1ac33d2808e1f069eee Mon Sep 17 00:00:00 2001 From: Ben Meadors Date: Mon, 17 Nov 2025 19:58:12 -0600 Subject: [PATCH] Fix ble rssi crash (#8661) * Fix BLE crash occuring when trying to get RSSI from Android with a bad connection handle * Cleanup --- src/nimble/NimbleBluetooth.cpp | 40 +++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/nimble/NimbleBluetooth.cpp b/src/nimble/NimbleBluetooth.cpp index 6238031f6..b09777de3 100644 --- a/src/nimble/NimbleBluetooth.cpp +++ b/src/nimble/NimbleBluetooth.cpp @@ -20,13 +20,14 @@ #include "PowerStatus.h" #endif -#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6) #if defined(CONFIG_NIMBLE_CPP_IDF) #include "host/ble_gap.h" #else #include "nimble/nimble/host/include/host/ble_gap.h" #endif +#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6) + namespace { constexpr uint16_t kPreferredBleMtu = 517; @@ -776,16 +777,35 @@ bool NimbleBluetooth::isConnected() int NimbleBluetooth::getRssi() { - if (bleServer && isConnected()) { - auto service = bleServer->getServiceByUUID(MESH_SERVICE_UUID); - uint16_t handle = service->getHandle(); -#ifdef NIMBLE_TWO - return NimBLEDevice::getClientByHandle(handle)->getRssi(); -#else - return NimBLEDevice::getClientByID(handle)->getRssi(); -#endif +#if defined(CONFIG_IDF_TARGET_ESP32S3) || defined(CONFIG_IDF_TARGET_ESP32C6) + if (!bleServer || !isConnected()) { + return 0; // No active BLE connection } - return 0; // FIXME figure out where to source this + + uint16_t connHandle = nimbleBluetoothConnHandle.load(); + + if (connHandle == BLE_HS_CONN_HANDLE_NONE) { + const auto peers = bleServer->getPeerDevices(); + if (!peers.empty()) { + connHandle = peers.front(); + nimbleBluetoothConnHandle = connHandle; + } + } + + if (connHandle == BLE_HS_CONN_HANDLE_NONE) { + return 0; // Connection handle not available yet + } + + int8_t rssi = 0; + const int rc = ble_gap_conn_rssi(connHandle, &rssi); + + if (rc == 0) { + return rssi; + } + LOG_DEBUG("BLE RSSI read failed, rc=%d", rc); +#endif + + return 0; } void NimbleBluetooth::setup()