BluetoothAdapter safeEnable,safeDisable

This commit is contained in:
Milos Kozak 2022-09-21 14:24:20 +02:00
parent 0a2c5fec87
commit ad152120ec
8 changed files with 58 additions and 32 deletions

View file

@ -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);

View file

@ -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
}

View file

@ -2,6 +2,9 @@ package info.nightscout.androidaps.extensions
import android.content.Intent
/**
* Safe version of getParcelableExtra depending on Android version running
*/
fun <T> Intent.safeGetParcelableExtra(name: String?, clazz: Class<T>): T? =
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getParcelableExtra(name, clazz)
else @Suppress("DEPRECATION") getParcelableExtra(name)

View file

@ -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

View file

@ -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)

View file

@ -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()
}

View file

@ -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);

View file

@ -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);