Skip to content

Commit

Permalink
restart bluetooth on app relaunch
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Jun 26, 2024
1 parent 70cc170 commit f08198a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
4 changes: 4 additions & 0 deletions app/src/main/kotlin/io/treehouses/remote/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -61,6 +62,9 @@ class MainApplication : Application() {
}

fun getCurrentBluetoothService(): BluetoothChatService? {
if (mChatService == null) {
mChatService = BluetoothChatService()
}
return mChatService
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit f08198a

Please sign in to comment.