From 488e0f807128559d131692a29385317c0b7b11fd Mon Sep 17 00:00:00 2001 From: NiroDev <23154768+NiroDeveloper@users.noreply.github.com> Date: Sun, 2 Jun 2024 21:29:53 +0200 Subject: [PATCH] Improve app shutdown --- .../bluetooth/BluetoothController.kt | 16 +++++++--------- .../bluetooth/BluetoothServiceCallback.kt | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothController.kt b/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothController.kt index 0ac0e5b..b6aaef1 100644 --- a/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothController.kt +++ b/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothController.kt @@ -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") } diff --git a/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothServiceCallback.kt b/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothServiceCallback.kt index 4f75500..e9917e1 100644 --- a/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothServiceCallback.kt +++ b/app/src/main/java/dev/niro/cameraremote/bluetooth/BluetoothServiceCallback.kt @@ -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 @@ -67,8 +79,6 @@ class BluetoothServiceCallback( if (available) { startAutoConnect() - - startRadarThread() } serviceStateListener.onServiceStateChange(available) @@ -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() }