Skip to content

Commit 64cf0f9

Browse files
mdecourcyclaude
andcommitted
fix: add timeout to BLE RSSI polling to prevent ANR
Uses withTimeout(5.seconds) instead of Dispatchers.IO to prevent the main thread from blocking indefinitely when readRssi() hangs. This approach: - Keeps the operation on the appropriate dispatcher (the BLE library manages threading) - Breaks out of the polling loop if RSSI read times out or fails - Prevents 5-second ANR without affecting BLE connection flow Previous approach (Dispatchers.IO) was interfering with connection state observation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 71d9caf commit 64cf0f9

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

app/src/main/java/com/geeksville/mesh/ui/connections/components/CurrentlyConnectedInfo.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ import androidx.compose.ui.text.style.TextOverflow
4141
import androidx.compose.ui.tooling.preview.PreviewLightDark
4242
import androidx.compose.ui.unit.dp
4343
import com.geeksville.mesh.model.DeviceListEntry
44+
import kotlinx.coroutines.TimeoutCancellationException
4445
import kotlinx.coroutines.delay
46+
import kotlinx.coroutines.withTimeout
4547
import org.jetbrains.compose.resources.stringResource
4648
import org.meshtastic.core.database.model.Node
4749
import org.meshtastic.core.strings.Res
@@ -72,10 +74,16 @@ fun CurrentlyConnectedInfo(
7274
if (bleDevice != null) {
7375
while (bleDevice.peripheral.isConnected) {
7476
try {
75-
rssi = bleDevice.peripheral.readRssi()
77+
rssi = withTimeout(5.seconds) {
78+
bleDevice.peripheral.readRssi()
79+
}
7680
delay(10.seconds)
81+
} catch (e: TimeoutCancellationException) {
82+
Timber.w("RSSI read timed out after 5 seconds")
83+
break
7784
} catch (e: Exception) {
7885
Timber.e(e, "Failed to read RSSI")
86+
break
7987
}
8088
}
8189
}

0 commit comments

Comments
 (0)