From f08198a6673c7efef231d1ddfe7ffa5b7dac781f Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Wed, 26 Jun 2024 18:39:43 +0300 Subject: [PATCH] restart bluetooth on app relaunch --- .../io/treehouses/remote/MainApplication.kt | 4 +++ .../remote/bases/BaseBluetoothChatService.kt | 31 ++++++++++--------- .../remote/network/BluetoothChatService.kt | 1 + 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/app/src/main/kotlin/io/treehouses/remote/MainApplication.kt b/app/src/main/kotlin/io/treehouses/remote/MainApplication.kt index dd05781c2..d327cf75d 100644 --- a/app/src/main/kotlin/io/treehouses/remote/MainApplication.kt +++ b/app/src/main/kotlin/io/treehouses/remote/MainApplication.kt @@ -23,6 +23,7 @@ class MainApplication : Application() { var logSent = false private lateinit var appLifecycleObserver: AppLifecycleObserver private lateinit var activityLifecycleTracker: AppLifecycleTracker +// private var bluetoothService: BluetoothChatService? = null override fun onCreate() { super.onCreate() @@ -61,6 +62,9 @@ class MainApplication : Application() { } fun getCurrentBluetoothService(): BluetoothChatService? { + if (mChatService == null) { + mChatService = BluetoothChatService() + } return mChatService } diff --git a/app/src/main/kotlin/io/treehouses/remote/bases/BaseBluetoothChatService.kt b/app/src/main/kotlin/io/treehouses/remote/bases/BaseBluetoothChatService.kt index 1c383ed4a..f3df42f5f 100644 --- a/app/src/main/kotlin/io/treehouses/remote/bases/BaseBluetoothChatService.kt +++ b/app/src/main/kotlin/io/treehouses/remote/bases/BaseBluetoothChatService.kt @@ -77,14 +77,16 @@ open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler? } protected fun startNotification() { - val disconnectIntent = Intent(DISCONNECT_ACTION) - val disconnectPendingIntent: PendingIntent = PendingIntent.getBroadcast(this, 0, disconnectIntent, FLAG_IMMUTABLE) + context?.let { + val disconnectIntent = Intent(DISCONNECT_ACTION) + val disconnectPendingIntent = PendingIntent.getBroadcast(this, 0, disconnectIntent, FLAG_IMMUTABLE) - val onClickIntent = Intent(this, InitialActivity::class.java) - val pendingClickIntent = PendingIntent.getActivity(this, 0, onClickIntent, FLAG_IMMUTABLE) + val onClickIntent = Intent(this, InitialActivity::class.java) + val pendingClickIntent = PendingIntent.getActivity(this, 0, onClickIntent, FLAG_IMMUTABLE) - val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, getString(R.string.bt_notification_ID)) - val notification: Notification = notificationBuilder.setOngoing(true) + val notificationBuilder: NotificationCompat.Builder = + NotificationCompat.Builder(this, getString(R.string.bt_notification_ID)) + val notification: Notification = notificationBuilder.setOngoing(true) .setContentTitle("Treehouses Remote is currently running") .setContentText("Connected to ${mDevice?.name}") .setPriority(NotificationCompat.PRIORITY_HIGH) @@ -93,14 +95,15 @@ open class BaseBluetoothChatService @JvmOverloads constructor(handler: Handler? .setContentIntent(pendingClickIntent) .addAction(R.drawable.bluetooth, "Disconnect", disconnectPendingIntent) .build() - ServiceCompat.startForeground(this,2, - notification, - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE - } else { - 0 - } - ) + ServiceCompat.startForeground(this, 2, + notification, + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE + } else { + 0 + } + ) + } } /** diff --git a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt index c13befc67..7e86dd575 100644 --- a/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt +++ b/app/src/main/kotlin/io/treehouses/remote/network/BluetoothChatService.kt @@ -60,6 +60,7 @@ class BluetoothChatService @JvmOverloads constructor(handler: Handler? = null, a @RequiresApi(Build.VERSION_CODES.TIRAMISU) override fun onCreate() { super.onCreate() + context = applicationContext val i = IntentFilter() i.addAction(DISCONNECT_ACTION) registerReceiver(receiver, i, RECEIVER_NOT_EXPORTED)