mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-04-30 18:16:42 +00:00
android: fix crash on some devices not properly closing socket
This commit is contained in:
@@ -2768,33 +2768,43 @@ class AirPodsService : Service(), SharedPreferences.OnSharedPreferenceChangeList
|
|||||||
|
|
||||||
while (socket.isConnected) {
|
while (socket.isConnected) {
|
||||||
socket.let { it ->
|
socket.let { it ->
|
||||||
val buffer = ByteArray(1024)
|
try {
|
||||||
val bytesRead = it.inputStream.read(buffer)
|
val buffer = ByteArray(1024)
|
||||||
var data: ByteArray
|
val bytesRead = it.inputStream.read(buffer)
|
||||||
if (bytesRead > 0) {
|
var data: ByteArray
|
||||||
data = buffer.copyOfRange(0, bytesRead)
|
if (bytesRead > 0) {
|
||||||
sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DATA).apply {
|
data = buffer.copyOfRange(0, bytesRead)
|
||||||
putExtra("data", buffer.copyOfRange(0, bytesRead))
|
sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DATA).apply {
|
||||||
setPackage(packageName)
|
putExtra("data", buffer.copyOfRange(0, bytesRead))
|
||||||
})
|
setPackage(packageName)
|
||||||
val bytes = buffer.copyOfRange(0, bytesRead)
|
})
|
||||||
val formattedHex = bytes.joinToString(" ") { "%02X".format(it) }
|
val bytes = buffer.copyOfRange(0, bytesRead)
|
||||||
|
val formattedHex = bytes.joinToString(" ") { "%02X".format(it) }
|
||||||
// CrossDevice.sendReceivedPacket(bytes)
|
// CrossDevice.sendReceivedPacket(bytes)
|
||||||
updateNotificationContent(
|
updateNotificationContent(
|
||||||
true,
|
true,
|
||||||
sharedPreferences.getString("name", device.name),
|
sharedPreferences.getString("name", device.name),
|
||||||
batteryNotification.getBattery()
|
batteryNotification.getBattery()
|
||||||
)
|
)
|
||||||
|
|
||||||
aacpManager.receivePacket(data)
|
aacpManager.receivePacket(data)
|
||||||
|
|
||||||
if (!isHeadTrackingData(data)) {
|
if (!isHeadTrackingData(data)) {
|
||||||
Log.d("AirPodsData", "Data received: $formattedHex")
|
Log.d("AirPodsData", "Data received: $formattedHex")
|
||||||
logPacket(data, "AirPods")
|
logPacket(data, "AirPods")
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (bytesRead == -1) {
|
||||||
|
Log.d("AirPods Service", "Socket closed (bytesRead = -1)")
|
||||||
|
sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED).apply {
|
||||||
|
setPackage(packageName)
|
||||||
|
})
|
||||||
|
aacpManager.disconnected()
|
||||||
|
return@launch
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
} else if (bytesRead == -1) {
|
Log.w(TAG, "Error reading data, we have probably disconnected.")
|
||||||
Log.d("AirPods Service", "Socket closed (bytesRead = -1)")
|
e.printStackTrace()
|
||||||
sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED).apply {
|
sendBroadcast(Intent(AirPodsNotifications.AIRPODS_DISCONNECTED).apply {
|
||||||
setPackage(packageName)
|
setPackage(packageName)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user