Skip to content

Commit

Permalink
Improve app shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
NiroDeveloper committed Jun 2, 2024
1 parent 8199f2c commit 488e0f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ object BluetoothController {
}

fun destroy(context: Context) {
bluetoothCallback?.hidDevice?.let {
try {
it.unregisterApp()
} catch (ex: SecurityException) {
Log.wtf(null, "Failed calling BluetoothHidDevice.unregisterApp(): $ex")
}
bluetoothCallback?.let { bluetoothCallback ->
bluetoothCallback.hidDevice?.let { hidDevice ->
val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter = bluetoothManager.adapter

val bluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val bluetoothAdapter = bluetoothManager.adapter
bluetoothAdapter.closeProfileProxy(BluetoothProfile.HID_DEVICE, hidDevice)
}

bluetoothAdapter.closeProfileProxy(BluetoothProfile.HID_DEVICE, it)
bluetoothCallback.destroy()

Log.i(null, "Destroyed bluetooth service")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,23 @@ class BluetoothServiceCallback(

hidDevice = proxy
hidCallback = registerApp(proxy)

startRadarThread()
}

override fun onServiceDisconnected(profile: Int) {
Log.d(null, "onServiceDisconnected($profile)")

destroy()
}

fun destroy() {
try {
hidDevice?.unregisterApp()
} catch (ex: SecurityException) {
Log.wtf(null, "Failed calling BluetoothHidDevice.unregisterApp(): $ex")
}

hidDevice = null
hidCallback = null
appRegistered = false
Expand All @@ -67,8 +79,6 @@ class BluetoothServiceCallback(

if (available) {
startAutoConnect()

startRadarThread()
}

serviceStateListener.onServiceStateChange(available)
Expand Down Expand Up @@ -132,7 +142,8 @@ class BluetoothServiceCallback(
Thread.sleep(sleepDelay)
}
}
thread.name = "Radar"
thread.isDaemon = true
thread.name = "Bluetooth Device Radar"
thread.priority = Thread.MIN_PRIORITY
thread.start()
}
Expand Down

0 comments on commit 488e0f8

Please sign in to comment.