android: fix the socket error notification showing up even when it connection suceeds

This commit is contained in:
Kavish Devar
2025-05-11 21:04:42 +05:30
parent 116f7dda92
commit 0611509782

View File

@@ -803,7 +803,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
notificationManager.notify(1, updatedNotification) notificationManager.notify(1, updatedNotification)
notificationManager.cancel(2) notificationManager.cancel(2)
} else { } else if (!socket.isConnected && isConnectedLocally) {
showSocketConnectionFailureNotification("Socket created, but not connected. Is the Bluetooth process hooked?") showSocketConnectionFailureNotification("Socket created, but not connected. Is the Bluetooth process hooked?")
} }
} }
@@ -1256,11 +1256,6 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
sharedPreferences.edit { sharedPreferences.edit {
putString("mac_address", macAddress) putString("mac_address", macAddress)
} }
updateNotificationContent(
true,
config.deviceName,
batteryNotification.getBattery()
)
} }
} else if (intent?.action == AirPodsNotifications.AIRPODS_DISCONNECTED) { } else if (intent?.action == AirPodsNotifications.AIRPODS_DISCONNECTED) {
device = null device = null
@@ -1444,7 +1439,13 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
withTimeout(5000L) { withTimeout(5000L) {
try { try {
socket.connect() socket.connect()
isConnectedLocally = true
this@AirPodsService.device = device this@AirPodsService.device = device
updateNotificationContent(
true,
config.deviceName,
batteryNotification.getBattery()
)
} catch (e: Exception) { } catch (e: Exception) {
showSocketConnectionFailureNotification("Socket created, but not connected. Is the Bluetooth process hooked?") showSocketConnectionFailureNotification("Socket created, but not connected. Is the Bluetooth process hooked?")
throw e throw e
@@ -1455,7 +1456,6 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
} }
} }
this@AirPodsService.device = device this@AirPodsService.device = device
isConnectedLocally = true
socket.let { it -> socket.let { it ->
it.outputStream.write(Enums.HANDSHAKE.value) it.outputStream.write(Enums.HANDSHAKE.value)
it.outputStream.flush() it.outputStream.flush()
@@ -1734,11 +1734,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
showSocketConnectionFailureNotification("Failed to establish connection: ${e.message}") showSocketConnectionFailureNotification("Failed to establish connection: ${e.message}")
isConnectedLocally = false isConnectedLocally = false
this@AirPodsService.device = device this@AirPodsService.device = device
updateNotificationContent( updateNotificationContent(false)
true,
config.deviceName,
null
)
} }
} }
} }
@@ -1798,7 +1794,7 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
CrossDevice.sendRemotePacket(CrossDevicePackets.AIRPODS_DATA_HEADER.packet + packet) CrossDevice.sendRemotePacket(CrossDevicePackets.AIRPODS_DATA_HEADER.packet + packet)
return return
} }
if (this::socket.isInitialized && socket.isConnected && socket.outputStream != null) { if (this::socket.isInitialized && socket.isConnected && socket.outputStream != null && isConnectedLocally) {
socket.outputStream?.write(packet) socket.outputStream?.write(packet)
socket.outputStream?.flush() socket.outputStream?.flush()
} else { } else {