@@ -40,12 +40,14 @@ import android.os.IBinder
4040import android.os.ParcelUuid
4141import android.util.Log
4242import android.widget.RemoteViews
43+ import androidx.compose.material3.ExperimentalMaterial3Api
4344import androidx.core.app.NotificationCompat
4445import kotlinx.coroutines.CoroutineScope
4546import kotlinx.coroutines.Dispatchers
4647import kotlinx.coroutines.delay
4748import kotlinx.coroutines.launch
4849import me.kavishdevar.aln.BatteryWidget
50+ import me.kavishdevar.aln.MainActivity
4951import me.kavishdevar.aln.R
5052import me.kavishdevar.aln.utils.AirPodsNotifications
5153import 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