From 35364a6685847fb1a7d8d567a80f4542644938ed Mon Sep 17 00:00:00 2001 From: andrekir Date: Wed, 3 Jan 2024 08:38:30 -0300 Subject: [PATCH] refactor: Bluetooth scan only when adapter is enabled --- .../com/geeksville/mesh/ui/SettingsFragment.kt | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt index fd2305e21..251d8b458 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/SettingsFragment.kt @@ -475,8 +475,10 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { } // per https://developer.android.com/guide/topics/connectivity/bluetooth/find-ble-devices + private var scanning = false private fun scanLeDevice() { - var scanning = false + if (!checkBTEnabled()) return + if (!hasCompanionDeviceApi) checkLocationEnabled() if (!scanning) { // Stops scanning after a pre-defined scan period. Handler(Looper.getMainLooper()).postDelayed({ @@ -500,8 +502,6 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { permissions -> if (permissions.entries.all { it.value }) { info("Bluetooth permissions granted") - checkBTEnabled() - if (!hasCompanionDeviceApi) checkLocationEnabled() scanLeDevice() } else { warn("Bluetooth permissions denied") @@ -512,11 +512,9 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { binding.changeRadioButton.setOnClickListener { debug("User clicked changeRadioButton") - scanLeDevice() val bluetoothPermissions = requireContext().getBluetoothPermissions() if (bluetoothPermissions.isEmpty()) { - checkBTEnabled() - if (!hasCompanionDeviceApi) checkLocationEnabled() + scanLeDevice() } else { requireContext().rationaleDialog( shouldShowRequestPermissionRationale(bluetoothPermissions) @@ -538,8 +536,8 @@ class SettingsFragment : ScreenFragment("Settings"), Logging { } } - private fun checkBTEnabled() { - if (bluetoothViewModel.enabled.value == false) { + private fun checkBTEnabled(): Boolean = (bluetoothViewModel.enabled.value == true).also { enabled -> + if (!enabled) { warn("Telling user bluetooth is disabled") model.showSnackbar(R.string.bluetooth_disabled) }