From ad152120ec9ae81b32b68069abf2ec77d643dc10 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 21 Sep 2022 14:24:20 +0200 Subject: [PATCH] BluetoothAdapter safeEnable,safeDisable --- .../androidaps/queue/QueueThread.kt | 8 ++--- .../extensions/BluetoothAdapterExtension.kt | 36 +++++++++++++++++++ .../androidaps/extensions/IntentExtension.kt | 3 ++ .../plugins/pump/common/ble/BlePreCheck.kt | 9 ++--- .../danars/activities/BLEScanActivity.kt | 14 +++----- .../activities/DiaconnG8BLEScanActivity.kt | 5 +-- .../activities/InsightPairingActivity.java | 5 +-- .../insight/utils/ConnectionEstablisher.java | 10 ++---- 8 files changed, 58 insertions(+), 32 deletions(-) create mode 100644 core/src/main/java/info/nightscout/androidaps/extensions/BluetoothAdapterExtension.kt diff --git a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt index ee3d8e188c..edf8fac07c 100644 --- a/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt +++ b/app/src/main/java/info/nightscout/androidaps/queue/QueueThread.kt @@ -8,6 +8,8 @@ import android.os.SystemClock import info.nightscout.androidaps.Constants import info.nightscout.androidaps.R import info.nightscout.androidaps.events.EventPumpStatusChanged +import info.nightscout.androidaps.extensions.safeDisable +import info.nightscout.androidaps.extensions.safeEnable import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.CommandQueue import info.nightscout.androidaps.interfaces.Config @@ -77,10 +79,8 @@ class QueueThread internal constructor( pump.disconnect("watchdog") SystemClock.sleep(1000) (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bluetoothAdapter -> - bluetoothAdapter.disable() - SystemClock.sleep(1000) - bluetoothAdapter.enable() - SystemClock.sleep(1000) + bluetoothAdapter.safeDisable(1000) + bluetoothAdapter.safeEnable(1000) } //start over again once after watchdog barked //Notification notification = new Notification(Notification.OLD_NSCLIENT, "Watchdog", Notification.URGENT); diff --git a/core/src/main/java/info/nightscout/androidaps/extensions/BluetoothAdapterExtension.kt b/core/src/main/java/info/nightscout/androidaps/extensions/BluetoothAdapterExtension.kt new file mode 100644 index 0000000000..b247800ef9 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/extensions/BluetoothAdapterExtension.kt @@ -0,0 +1,36 @@ +package info.nightscout.androidaps.extensions + +import android.bluetooth.BluetoothAdapter +import android.os.SystemClock + +/** + * @param waitMilliseconds if !=0 wait after enable() + * @param after Runnable to execute after enable() + * + * @return true if enable was executed or not necessary + */ +fun BluetoothAdapter.safeEnable(waitMilliseconds: Long = 0, after: Runnable? = null): Boolean = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) false + else @Suppress("DEPRECATION") { + if (!isEnabled) { + val result = enable() + if (waitMilliseconds != 0L) SystemClock.sleep(waitMilliseconds) + after?.run() + result + } else true + } + +/** + * @param waitMilliseconds if !=0 wait after disable() + * + * @return true if disable was executed or not necessary + */ +fun BluetoothAdapter.safeDisable(waitMilliseconds: Long = 0): Boolean = + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) false + else @Suppress("DEPRECATION") { + if (isEnabled) { + val result = disable() + if (waitMilliseconds != 0L) SystemClock.sleep(waitMilliseconds) + result + } else true + } \ No newline at end of file diff --git a/core/src/main/java/info/nightscout/androidaps/extensions/IntentExtension.kt b/core/src/main/java/info/nightscout/androidaps/extensions/IntentExtension.kt index a5420b26a5..32bff5cabf 100644 --- a/core/src/main/java/info/nightscout/androidaps/extensions/IntentExtension.kt +++ b/core/src/main/java/info/nightscout/androidaps/extensions/IntentExtension.kt @@ -2,6 +2,9 @@ package info.nightscout.androidaps.extensions import android.content.Intent +/** + * Safe version of getParcelableExtra depending on Android version running + */ fun Intent.safeGetParcelableExtra(name: String?, clazz: Class): T? = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getParcelableExtra(name, clazz) else @Suppress("DEPRECATION") getParcelableExtra(name) diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt index 3d96e24bc4..6b82a07878 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/common/ble/BlePreCheck.kt @@ -7,14 +7,14 @@ import android.content.Intent import android.content.pm.PackageManager import android.location.LocationManager import android.os.Build -import android.os.SystemClock import android.provider.Settings import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import info.nightscout.androidaps.core.R -import info.nightscout.androidaps.utils.alertDialogs.OKDialog +import info.nightscout.androidaps.extensions.safeEnable import info.nightscout.androidaps.interfaces.ResourceHelper +import info.nightscout.androidaps.utils.alertDialogs.OKDialog import javax.inject.Inject import javax.inject.Singleton @@ -54,10 +54,7 @@ class BlePreCheck @Inject constructor( val bluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter // Ensures Bluetooth is available on the device and it is enabled. If not, // displays a dialog requesting user permission to enable Bluetooth. - if (bluetoothAdapter?.isEnabled != true) { - bluetoothAdapter?.enable() - SystemClock.sleep(3000) - } + bluetoothAdapter?.safeEnable(3000) if (bluetoothAdapter?.isEnabled != true) { OKDialog.show(activity, rh.gs(R.string.message), rh.gs(R.string.ble_not_enabled)) return false diff --git a/danars/src/main/java/info/nightscout/androidaps/danars/activities/BLEScanActivity.kt b/danars/src/main/java/info/nightscout/androidaps/danars/activities/BLEScanActivity.kt index 5f56ddc2b7..a6cface7dc 100644 --- a/danars/src/main/java/info/nightscout/androidaps/danars/activities/BLEScanActivity.kt +++ b/danars/src/main/java/info/nightscout/androidaps/danars/activities/BLEScanActivity.kt @@ -24,6 +24,7 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity import info.nightscout.androidaps.danars.R import info.nightscout.androidaps.danars.databinding.DanarsBlescannerActivityBinding import info.nightscout.androidaps.danars.events.EventDanaRSDeviceChange +import info.nightscout.androidaps.extensions.safeEnable import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.shared.sharedPreferences.SP @@ -62,7 +63,7 @@ class BLEScanActivity : NoSplashAppCompatActivity() { super.onResume() if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { - if (bluetoothAdapter?.isEnabled != true) bluetoothAdapter?.enable() + bluetoothAdapter?.safeEnable() startScan() } else { ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission)) @@ -180,20 +181,13 @@ class BLEScanActivity : NoSplashAppCompatActivity() { if (other !is BluetoothDeviceItem) { return false } - return stringEquals(device.address, other.device.address) - } - - private fun stringEquals(arg1: String, arg2: String): Boolean { - return try { - arg1 == arg2 - } catch (e: Exception) { - false - } + return device.address == other.device.address } override fun hashCode(): Int = device.hashCode() } + @Suppress("RegExpSimplifiable") private fun isSNCheck(sn: String): Boolean { val regex = "^([a-zA-Z]{3})([0-9]{5})([a-zA-Z]{2})$" val p = Pattern.compile(regex) diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/activities/DiaconnG8BLEScanActivity.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/activities/DiaconnG8BLEScanActivity.kt index 05fbebeaf1..1dc392ba5b 100644 --- a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/activities/DiaconnG8BLEScanActivity.kt +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/activities/DiaconnG8BLEScanActivity.kt @@ -27,10 +27,11 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity import info.nightscout.androidaps.diaconn.R import info.nightscout.androidaps.diaconn.databinding.DiaconnG8BlescannerActivityBinding import info.nightscout.androidaps.diaconn.events.EventDiaconnG8DeviceChange +import info.nightscout.androidaps.extensions.safeEnable import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.shared.sharedPreferences.SP -import java.util.* +import java.util.UUID import javax.inject.Inject class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() { @@ -67,7 +68,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) { bluetoothAdapter?.let { bluetoothAdapter -> - if (!bluetoothAdapter.isEnabled) bluetoothAdapter.enable() + bluetoothAdapter.safeEnable() bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner startScan() } diff --git a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java index 5a0cc24d68..e88086499b 100644 --- a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java +++ b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/activities/InsightPairingActivity.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.pump.insight.activities; +import info.nightscout.androidaps.extensions.BluetoothAdapterExtensionKt; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; @@ -33,13 +34,13 @@ import java.util.List; import javax.inject.Inject; import info.nightscout.androidaps.activities.NoSplashAppCompatActivity; +import info.nightscout.androidaps.extensions.IntentExtensionKt; import info.nightscout.androidaps.insight.R; import info.nightscout.androidaps.interfaces.PumpSync; import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck; import info.nightscout.androidaps.plugins.pump.insight.connection_service.InsightConnectionService; import info.nightscout.androidaps.plugins.pump.insight.descriptors.InsightState; import info.nightscout.androidaps.plugins.pump.insight.utils.ExceptionTranslator; -import info.nightscout.androidaps.extensions.IntentExtensionKt; public class InsightPairingActivity extends NoSplashAppCompatActivity implements InsightConnectionService.StateCallback, View.OnClickListener, InsightConnectionService.ExceptionCallback { @@ -190,7 +191,7 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements if (!scanning) { BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); if (bluetoothAdapter != null) { - if (!bluetoothAdapter.isEnabled()) bluetoothAdapter.enable(); + BluetoothAdapterExtensionKt.safeEnable(bluetoothAdapter, 0, null); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); intentFilter.addAction(BluetoothDevice.ACTION_FOUND); diff --git a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ConnectionEstablisher.java b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ConnectionEstablisher.java index 7c957bd866..fa23118e77 100644 --- a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ConnectionEstablisher.java +++ b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/utils/ConnectionEstablisher.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.lang.reflect.Method; import java.util.UUID; +import info.nightscout.androidaps.extensions.BluetoothAdapterExtensionKt; public class ConnectionEstablisher extends Thread { private final Callback callback; @@ -26,14 +27,7 @@ public class ConnectionEstablisher extends Thread { @Override public void run() { - try { - if (!bluetoothAdapter.isEnabled()) { - bluetoothAdapter.enable(); - Thread.sleep(2000); - } - } catch (InterruptedException ignored) { - return; - } + BluetoothAdapterExtensionKt.safeEnable(bluetoothAdapter, 2000, null); if (forPairing && bluetoothDevice.getBondState() != BluetoothDevice.BOND_NONE) { try { Method removeBond = bluetoothDevice.getClass().getMethod("removeBond", (Class[]) null);