Skip to content

Commit aadcaf1

Browse files
teng-linclaude
andcommitted
fix(android): detect stale socket in connectToSocket guard
isConnectedLocally can be stale after a remote disconnect because connectionReceiver sets it true on ACL_CONNECTED before connectToSocket runs. Now verify the socket is actually alive by probing inputStream before skipping reconnection. If the socket is dead, reset the flag and proceed with a fresh connection. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 725cfb7 commit aadcaf1

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

android/app/src/main/java/me/kavishdevar/librepods/services/AirPodsService.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2427,7 +2427,14 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
24272427
Log.d(TAG, "<LogCollector:Start> Connecting to socket")
24282428
HiddenApiBypass.addHiddenApiExemptions("Landroid/bluetooth/BluetoothSocket;")
24292429
val uuid: ParcelUuid = ParcelUuid.fromString("74ec2172-0bad-4d01-8f77-997b2be0722a")
2430-
if (!isConnectedLocally) {
2430+
val socketActuallyAlive = isConnectedLocally && this::socket.isInitialized &&
2431+
try { socket.inputStream.available(); true } catch (_: Exception) { false }
2432+
if (!socketActuallyAlive) {
2433+
if (isConnectedLocally) {
2434+
Log.d(TAG, "isConnectedLocally was true but socket is dead, resetting")
2435+
isConnectedLocally = false
2436+
if (this::socket.isInitialized) try { socket.close() } catch (_: Exception) { }
2437+
}
24312438
socket = try {
24322439
createBluetoothSocket(device, uuid)
24332440
} catch (e: Exception) {

0 commit comments

Comments
 (0)