BluetoothAdapter safeEnable,safeDisable
This commit is contained in:
parent
0a2c5fec87
commit
ad152120ec
|
@ -8,6 +8,8 @@ import android.os.SystemClock
|
||||||
import info.nightscout.androidaps.Constants
|
import info.nightscout.androidaps.Constants
|
||||||
import info.nightscout.androidaps.R
|
import info.nightscout.androidaps.R
|
||||||
import info.nightscout.androidaps.events.EventPumpStatusChanged
|
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.ActivePlugin
|
||||||
import info.nightscout.androidaps.interfaces.CommandQueue
|
import info.nightscout.androidaps.interfaces.CommandQueue
|
||||||
import info.nightscout.androidaps.interfaces.Config
|
import info.nightscout.androidaps.interfaces.Config
|
||||||
|
@ -77,10 +79,8 @@ class QueueThread internal constructor(
|
||||||
pump.disconnect("watchdog")
|
pump.disconnect("watchdog")
|
||||||
SystemClock.sleep(1000)
|
SystemClock.sleep(1000)
|
||||||
(context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bluetoothAdapter ->
|
(context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bluetoothAdapter ->
|
||||||
bluetoothAdapter.disable()
|
bluetoothAdapter.safeDisable(1000)
|
||||||
SystemClock.sleep(1000)
|
bluetoothAdapter.safeEnable(1000)
|
||||||
bluetoothAdapter.enable()
|
|
||||||
SystemClock.sleep(1000)
|
|
||||||
}
|
}
|
||||||
//start over again once after watchdog barked
|
//start over again once after watchdog barked
|
||||||
//Notification notification = new Notification(Notification.OLD_NSCLIENT, "Watchdog", Notification.URGENT);
|
//Notification notification = new Notification(Notification.OLD_NSCLIENT, "Watchdog", Notification.URGENT);
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -2,6 +2,9 @@ package info.nightscout.androidaps.extensions
|
||||||
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Safe version of getParcelableExtra depending on Android version running
|
||||||
|
*/
|
||||||
fun <T> Intent.safeGetParcelableExtra(name: String?, clazz: Class<T>): T? =
|
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)
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) getParcelableExtra(name, clazz)
|
||||||
else @Suppress("DEPRECATION") getParcelableExtra(name)
|
else @Suppress("DEPRECATION") getParcelableExtra(name)
|
||||||
|
|
|
@ -7,14 +7,14 @@ import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.location.LocationManager
|
import android.location.LocationManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.SystemClock
|
|
||||||
import android.provider.Settings
|
import android.provider.Settings
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import info.nightscout.androidaps.core.R
|
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.interfaces.ResourceHelper
|
||||||
|
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@ -54,10 +54,7 @@ class BlePreCheck @Inject constructor(
|
||||||
val bluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter
|
val bluetoothAdapter = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter
|
||||||
// Ensures Bluetooth is available on the device and it is enabled. If not,
|
// Ensures Bluetooth is available on the device and it is enabled. If not,
|
||||||
// displays a dialog requesting user permission to enable Bluetooth.
|
// displays a dialog requesting user permission to enable Bluetooth.
|
||||||
if (bluetoothAdapter?.isEnabled != true) {
|
bluetoothAdapter?.safeEnable(3000)
|
||||||
bluetoothAdapter?.enable()
|
|
||||||
SystemClock.sleep(3000)
|
|
||||||
}
|
|
||||||
if (bluetoothAdapter?.isEnabled != true) {
|
if (bluetoothAdapter?.isEnabled != true) {
|
||||||
OKDialog.show(activity, rh.gs(R.string.message), rh.gs(R.string.ble_not_enabled))
|
OKDialog.show(activity, rh.gs(R.string.message), rh.gs(R.string.ble_not_enabled))
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -24,6 +24,7 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
|
||||||
import info.nightscout.androidaps.danars.R
|
import info.nightscout.androidaps.danars.R
|
||||||
import info.nightscout.androidaps.danars.databinding.DanarsBlescannerActivityBinding
|
import info.nightscout.androidaps.danars.databinding.DanarsBlescannerActivityBinding
|
||||||
import info.nightscout.androidaps.danars.events.EventDanaRSDeviceChange
|
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.plugins.pump.common.ble.BlePreCheck
|
||||||
import info.nightscout.androidaps.utils.ToastUtils
|
import info.nightscout.androidaps.utils.ToastUtils
|
||||||
import info.nightscout.shared.sharedPreferences.SP
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
|
@ -62,7 +63,7 @@ class BLEScanActivity : NoSplashAppCompatActivity() {
|
||||||
super.onResume()
|
super.onResume()
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
|
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()
|
startScan()
|
||||||
} else {
|
} else {
|
||||||
ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission))
|
ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission))
|
||||||
|
@ -180,20 +181,13 @@ class BLEScanActivity : NoSplashAppCompatActivity() {
|
||||||
if (other !is BluetoothDeviceItem) {
|
if (other !is BluetoothDeviceItem) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return stringEquals(device.address, other.device.address)
|
return device.address == other.device.address
|
||||||
}
|
|
||||||
|
|
||||||
private fun stringEquals(arg1: String, arg2: String): Boolean {
|
|
||||||
return try {
|
|
||||||
arg1 == arg2
|
|
||||||
} catch (e: Exception) {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int = device.hashCode()
|
override fun hashCode(): Int = device.hashCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Suppress("RegExpSimplifiable")
|
||||||
private fun isSNCheck(sn: String): Boolean {
|
private fun isSNCheck(sn: String): Boolean {
|
||||||
val regex = "^([a-zA-Z]{3})([0-9]{5})([a-zA-Z]{2})$"
|
val regex = "^([a-zA-Z]{3})([0-9]{5})([a-zA-Z]{2})$"
|
||||||
val p = Pattern.compile(regex)
|
val p = Pattern.compile(regex)
|
||||||
|
|
|
@ -27,10 +27,11 @@ import info.nightscout.androidaps.activities.NoSplashAppCompatActivity
|
||||||
import info.nightscout.androidaps.diaconn.R
|
import info.nightscout.androidaps.diaconn.R
|
||||||
import info.nightscout.androidaps.diaconn.databinding.DiaconnG8BlescannerActivityBinding
|
import info.nightscout.androidaps.diaconn.databinding.DiaconnG8BlescannerActivityBinding
|
||||||
import info.nightscout.androidaps.diaconn.events.EventDiaconnG8DeviceChange
|
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.plugins.pump.common.ble.BlePreCheck
|
||||||
import info.nightscout.androidaps.utils.ToastUtils
|
import info.nightscout.androidaps.utils.ToastUtils
|
||||||
import info.nightscout.shared.sharedPreferences.SP
|
import info.nightscout.shared.sharedPreferences.SP
|
||||||
import java.util.*
|
import java.util.UUID
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() {
|
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) {
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) {
|
||||||
bluetoothAdapter?.let { bluetoothAdapter ->
|
bluetoothAdapter?.let { bluetoothAdapter ->
|
||||||
if (!bluetoothAdapter.isEnabled) bluetoothAdapter.enable()
|
bluetoothAdapter.safeEnable()
|
||||||
bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
|
bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
|
||||||
startScan()
|
startScan()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package info.nightscout.androidaps.plugins.pump.insight.activities;
|
package info.nightscout.androidaps.plugins.pump.insight.activities;
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.extensions.BluetoothAdapterExtensionKt;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
import android.bluetooth.BluetoothManager;
|
import android.bluetooth.BluetoothManager;
|
||||||
|
@ -33,13 +34,13 @@ import java.util.List;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
|
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
|
||||||
|
import info.nightscout.androidaps.extensions.IntentExtensionKt;
|
||||||
import info.nightscout.androidaps.insight.R;
|
import info.nightscout.androidaps.insight.R;
|
||||||
import info.nightscout.androidaps.interfaces.PumpSync;
|
import info.nightscout.androidaps.interfaces.PumpSync;
|
||||||
import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck;
|
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.connection_service.InsightConnectionService;
|
||||||
import info.nightscout.androidaps.plugins.pump.insight.descriptors.InsightState;
|
import info.nightscout.androidaps.plugins.pump.insight.descriptors.InsightState;
|
||||||
import info.nightscout.androidaps.plugins.pump.insight.utils.ExceptionTranslator;
|
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 {
|
public class InsightPairingActivity extends NoSplashAppCompatActivity implements InsightConnectionService.StateCallback, View.OnClickListener, InsightConnectionService.ExceptionCallback {
|
||||||
|
|
||||||
|
@ -190,7 +191,7 @@ public class InsightPairingActivity extends NoSplashAppCompatActivity implements
|
||||||
if (!scanning) {
|
if (!scanning) {
|
||||||
BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
|
BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
|
||||||
if (bluetoothAdapter != null) {
|
if (bluetoothAdapter != null) {
|
||||||
if (!bluetoothAdapter.isEnabled()) bluetoothAdapter.enable();
|
BluetoothAdapterExtensionKt.safeEnable(bluetoothAdapter, 0, null);
|
||||||
IntentFilter intentFilter = new IntentFilter();
|
IntentFilter intentFilter = new IntentFilter();
|
||||||
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
||||||
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
|
intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.io.IOException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import info.nightscout.androidaps.extensions.BluetoothAdapterExtensionKt;
|
||||||
public class ConnectionEstablisher extends Thread {
|
public class ConnectionEstablisher extends Thread {
|
||||||
|
|
||||||
private final Callback callback;
|
private final Callback callback;
|
||||||
|
@ -26,14 +27,7 @@ public class ConnectionEstablisher extends Thread {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
BluetoothAdapterExtensionKt.safeEnable(bluetoothAdapter, 2000, null);
|
||||||
if (!bluetoothAdapter.isEnabled()) {
|
|
||||||
bluetoothAdapter.enable();
|
|
||||||
Thread.sleep(2000);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (forPairing && bluetoothDevice.getBondState() != BluetoothDevice.BOND_NONE) {
|
if (forPairing && bluetoothDevice.getBondState() != BluetoothDevice.BOND_NONE) {
|
||||||
try {
|
try {
|
||||||
Method removeBond = bluetoothDevice.getClass().getMethod("removeBond", (Class[]) null);
|
Method removeBond = bluetoothDevice.getClass().getMethod("removeBond", (Class[]) null);
|
||||||
|
|
Loading…
Reference in a new issue