Skip to content

Commit 7ed8f9b

Browse files
committed
fix: when both airpods are not worn, the settings screen would say not connected
1 parent 140ef08 commit 7ed8f9b

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ import android.os.IBinder
4040
import android.os.ParcelUuid
4141
import android.util.Log
4242
import android.widget.RemoteViews
43+
import androidx.compose.material3.ExperimentalMaterial3Api
4344
import androidx.core.app.NotificationCompat
4445
import kotlinx.coroutines.CoroutineScope
4546
import kotlinx.coroutines.Dispatchers
4647
import kotlinx.coroutines.delay
4748
import kotlinx.coroutines.launch
4849
import me.kavishdevar.aln.BatteryWidget
50+
import me.kavishdevar.aln.MainActivity
4951
import me.kavishdevar.aln.R
5052
import me.kavishdevar.aln.utils.AirPodsNotifications
5153
import me.kavishdevar.aln.utils.Battery
@@ -67,10 +69,18 @@ object ServiceManager {
6769
fun setService(service: AirPodsService?) {
6870
this.service = service
6971
}
72+
@OptIn(ExperimentalMaterial3Api::class)
7073
@Synchronized
7174
fun restartService(context: Context) {
7275
service?.stopSelf()
73-
context.startService(Intent(context, AirPodsService::class.java))
76+
Log.d("ServiceManager", "Restarting service, service is null: ${service == null}")
77+
val intent = Intent(context, AirPodsService::class.java)
78+
context.stopService(intent)
79+
CoroutineScope(Dispatchers.IO).launch {
80+
delay(1000)
81+
context.startService(intent)
82+
context.startActivity(Intent(context, MainActivity::class.java))
83+
}
7484
}
7585
}
7686

@@ -119,13 +129,6 @@ class AirPodsService: Service() {
119129
context?.sendBroadcast(intent)
120130
}
121131
}
122-
if (BluetoothDevice.ACTION_ACL_DISCONNECTED == action
123-
|| BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED == action
124-
) {
125-
context?.sendBroadcast(
126-
Intent(AirPodsNotifications.Companion.AIRPODS_DISCONNECTED)
127-
)
128-
}
129132
}
130133
}
131134
}
@@ -256,8 +259,6 @@ class AirPodsService: Service() {
256259
// .setOngoing(true)
257260
// .build()
258261

259-
// Instead have something like L: 50% | R: 50% | C: 12% with the emojis, without using remote views, in thte title itself.
260-
261262
updatedNotification = NotificationCompat.Builder(this, "background_service_status")
262263
.setSmallIcon(R.drawable.airpods)
263264
.setContentTitle("""L: ${batteryList?.find { it.component == BatteryComponent.LEFT }?.let {
@@ -474,7 +475,6 @@ class AirPodsService: Service() {
474475
Log.d("AirPods Data", "Data received: $formattedHex")
475476
} else if (bytesRead == -1) {
476477
Log.d("AirPods Service", "Socket closed (bytesRead = -1)")
477-
// socket.close()
478478
sendBroadcast(Intent(AirPodsNotifications.Companion.AIRPODS_DISCONNECTED))
479479
return@launch
480480
}

0 commit comments

Comments
 (0)