diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7bc428a968..148a476141 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -789,7 +789,6 @@ Application needs system window permission for notifications Application needs location permission for BT scan and WiFi identification Application needs storage permission to be able store log files and export settings - Application needs bluetooth permission Request Open navigation Close navigation diff --git a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBTDevice.kt b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBTDevice.kt index bb0c3848f1..02133d03d2 100644 --- a/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBTDevice.kt +++ b/automation/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerBTDevice.kt @@ -1,8 +1,11 @@ package info.nightscout.androidaps.plugins.general.automation.triggers +import android.Manifest import android.bluetooth.BluetoothManager import android.content.Context +import android.content.pm.PackageManager import android.widget.LinearLayout +import androidx.core.app.ActivityCompat import com.google.common.base.Optional import dagger.android.HasAndroidInjector import info.nightscout.androidaps.automation.R @@ -13,9 +16,9 @@ import info.nightscout.androidaps.plugins.general.automation.elements.InputDropd import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel import info.nightscout.androidaps.utils.JsonHelper +import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.shared.logging.LTag import org.json.JSONObject -import java.util.* import javax.inject.Inject class TriggerBTDevice(injector: HasAndroidInjector) : Trigger(injector) { @@ -74,7 +77,11 @@ class TriggerBTDevice(injector: HasAndroidInjector) : Trigger(injector) { // Get the list of paired BT devices to use in dropdown menu private fun devicesPaired(): ArrayList { val s = ArrayList() - (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.bondedDevices?.forEach { s.add(it.name) } + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { + (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.bondedDevices?.forEach { s.add(it.name) } + } else { + ToastUtils.errorToast(context, context.getString(R.string.needconnectpermission)) + } return s } diff --git a/core/src/main/AndroidManifest.xml b/core/src/main/AndroidManifest.xml index 0f37944ddb..083c7f30cf 100644 --- a/core/src/main/AndroidManifest.xml +++ b/core/src/main/AndroidManifest.xml @@ -8,6 +8,8 @@ + + = Build.VERSION_CODES.S) { - if (ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_CONNECT") != PackageManager.PERMISSION_GRANTED || - ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_SCAN") != PackageManager.PERMISSION_GRANTED + if (ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED || + ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED ) { - //ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), PERMISSION_REQUEST_BLUETOOTH) - ActivityCompat.requestPermissions(activity, arrayOf("android.permission.BLUETOOTH_SCAN", "android.permission.BLUETOOTH_CONNECT"), PERMISSION_REQUEST_BLUETOOTH) + ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT), PERMISSION_REQUEST_BLUETOOTH) return false } } diff --git a/core/src/main/java/info/nightscout/androidaps/utils/ui/BluetoothDevicePreference.kt b/core/src/main/java/info/nightscout/androidaps/utils/ui/BluetoothDevicePreference.kt new file mode 100644 index 0000000000..007d8ad412 --- /dev/null +++ b/core/src/main/java/info/nightscout/androidaps/utils/ui/BluetoothDevicePreference.kt @@ -0,0 +1,32 @@ +package info.nightscout.androidaps.utils.ui + +import android.Manifest +import kotlin.jvm.JvmOverloads +import android.bluetooth.BluetoothManager +import android.content.Context +import android.content.pm.PackageManager +import android.util.AttributeSet +import androidx.core.app.ActivityCompat +import androidx.preference.ListPreference +import info.nightscout.androidaps.core.R +import info.nightscout.androidaps.utils.ToastUtils +import java.util.* + +class BluetoothDevicePreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ListPreference(context, attrs) { + + init { + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { + val devices = Vector() + (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bta -> + for (dev in bta.bondedDevices) + dev.name?.let { name -> devices.add(name) } + } + entries = devices.toTypedArray() + entryValues = devices.toTypedArray() + } else { + entries = emptyArray() + entryValues = emptyArray() + ToastUtils.errorToast(context, context.getString(R.string.needconnectpermission)) + } + } +} \ No newline at end of file diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 3f5832b0cd..256d1c8b8b 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -601,6 +601,7 @@ Autotune runned and profile automatically switched Error during last Autotune run Another run of Autotune is detected, run cancelled + Application needs bluetooth permission %1$d day diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/BluetoothDevicePreference.kt b/danar/src/main/java/info/nightscout/androidaps/danar/BluetoothDevicePreference.kt deleted file mode 100644 index 41b090bc72..0000000000 --- a/danar/src/main/java/info/nightscout/androidaps/danar/BluetoothDevicePreference.kt +++ /dev/null @@ -1,22 +0,0 @@ -package info.nightscout.androidaps.danar - -import kotlin.jvm.JvmOverloads -import android.bluetooth.BluetoothAdapter -import android.bluetooth.BluetoothDevice -import android.bluetooth.BluetoothManager -import android.content.Context -import android.util.AttributeSet -import androidx.preference.ListPreference -import java.util.* - -class BluetoothDevicePreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ListPreference(context, attrs) { - init { - val entries = Vector() - (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bta -> - for (dev in bta.bondedDevices) - dev.name?.let { name -> entries.add(name) } - } - setEntries(entries.toTypedArray()) - entryValues = entries.toTypedArray() - } -} \ No newline at end of file diff --git a/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java b/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java index 2e0b01350c..2f7afdafe8 100644 --- a/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java +++ b/danar/src/main/java/info/nightscout/androidaps/danar/services/AbstractDanaRExecutionService.java @@ -1,14 +1,18 @@ package info.nightscout.androidaps.danar.services; +import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.IBinder; import android.os.SystemClock; +import androidx.core.app.ActivityCompat; + import java.io.IOException; import java.util.Set; import java.util.UUID; @@ -41,8 +45,7 @@ import info.nightscout.androidaps.events.EventPumpStatusChanged; import info.nightscout.androidaps.interfaces.ActivePlugin; import info.nightscout.androidaps.interfaces.Profile; import info.nightscout.androidaps.interfaces.PumpSync; -import info.nightscout.shared.logging.AAPSLogger; -import info.nightscout.shared.logging.LTag; +import info.nightscout.androidaps.interfaces.ResourceHelper; import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress; @@ -50,8 +53,9 @@ import info.nightscout.androidaps.plugins.general.overview.notifications.Notific import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.ToastUtils; -import info.nightscout.androidaps.interfaces.ResourceHelper; import info.nightscout.androidaps.utils.rx.AapsSchedulers; +import info.nightscout.shared.logging.AAPSLogger; +import info.nightscout.shared.logging.LTag; import info.nightscout.shared.sharedPreferences.SP; import io.reactivex.rxjava3.disposables.CompositeDisposable; @@ -191,30 +195,32 @@ public abstract class AbstractDanaRExecutionService extends DaggerService { protected void getBTSocketForSelectedPump() { mDevName = sp.getString(R.string.key_danar_bt_name, ""); - BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { + BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); - if (bluetoothAdapter != null) { - Set bondedDevices = bluetoothAdapter.getBondedDevices(); + if (bluetoothAdapter != null) { + Set bondedDevices = bluetoothAdapter.getBondedDevices(); - if (bondedDevices != null) - for (BluetoothDevice device : bondedDevices) { - if (mDevName.equals(device.getName())) { - mBTDevice = device; - try { - mRfcommSocket = mBTDevice.createRfcommSocketToServiceRecord(SPP_UUID); - } catch (IOException e) { - aapsLogger.error("Error creating socket: ", e); + if (bondedDevices != null) + for (BluetoothDevice device : bondedDevices) { + if (mDevName.equals(device.getName())) { + mBTDevice = device; + try { + mRfcommSocket = mBTDevice.createRfcommSocketToServiceRecord(SPP_UUID); + } catch (IOException e) { + aapsLogger.error("Error creating socket: ", e); + } + break; } - break; } - } + } else { + ToastUtils.INSTANCE.showToastInUiThread(context.getApplicationContext(), rh.gs(R.string.nobtadapter)); + } + if (mBTDevice == null) { + ToastUtils.INSTANCE.showToastInUiThread(context.getApplicationContext(), rh.gs(R.string.devicenotfound)); + } } else { - ToastUtils.INSTANCE.showToastInUiThread(context.getApplicationContext(), - rh.gs(R.string.nobtadapter)); - } - if (mBTDevice == null) { - ToastUtils.INSTANCE.showToastInUiThread(context.getApplicationContext(), - rh.gs(R.string.devicenotfound)); + ToastUtils.INSTANCE.errorToast(context, context.getString(R.string.needconnectpermission)); } } diff --git a/danar/src/main/res/xml/pref_danar.xml b/danar/src/main/res/xml/pref_danar.xml index c7d45b8f12..98b1f51b37 100644 --- a/danar/src/main/res/xml/pref_danar.xml +++ b/danar/src/main/res/xml/pref_danar.xml @@ -8,7 +8,7 @@ android:title="@string/danar_pump_settings" app:initialExpandedChildrenCount="0"> - diff --git a/danar/src/main/res/xml/pref_danarkorean.xml b/danar/src/main/res/xml/pref_danarkorean.xml index 96479baf58..5b3372e2ed 100644 --- a/danar/src/main/res/xml/pref_danarkorean.xml +++ b/danar/src/main/res/xml/pref_danarkorean.xml @@ -8,7 +8,7 @@ android:title="@string/danar_pump_settings" app:initialExpandedChildrenCount="0"> - diff --git a/danar/src/main/res/xml/pref_danarv2.xml b/danar/src/main/res/xml/pref_danarv2.xml index f18ed77e4a..f16c06afb6 100644 --- a/danar/src/main/res/xml/pref_danarv2.xml +++ b/danar/src/main/res/xml/pref_danarv2.xml @@ -8,7 +8,7 @@ android:title="@string/danar_pump_settings" app:initialExpandedChildrenCount="0"> - diff --git a/danars/src/main/AndroidManifest.xml b/danars/src/main/AndroidManifest.xml index c3ef8bc727..108cb94048 100644 --- a/danars/src/main/AndroidManifest.xml +++ b/danars/src/main/AndroidManifest.xml @@ -5,6 +5,8 @@ + + - if (!bluetoothAdapter.isEnabled) bluetoothAdapter.enable() - bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner - startScan() + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) { + bluetoothAdapter?.let { bluetoothAdapter -> + if (!bluetoothAdapter.isEnabled) bluetoothAdapter.enable() + bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner + startScan() + } + } else { + ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission)) } } override fun onPause() { super.onPause() - stopScan() + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) { + stopScan() + } } + @SuppressLint("MissingPermission") private fun startScan() = try { val filters: MutableList = ArrayList() @@ -86,16 +96,21 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() { .build() bluetoothLeScanner?.startScan(filters, settings, mBleScanCallback) - } catch (e: IllegalStateException) { + } catch (ignored: IllegalStateException) { } // ignore BT not on + @SuppressLint("MissingPermission") private fun stopScan() = try { bluetoothLeScanner?.stopScan(mBleScanCallback) - } catch (e: IllegalStateException) { + } catch (ignored: IllegalStateException) { } // ignore BT not on private fun addBleDevice(device: BluetoothDevice?) { + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission)) + return + } if (device == null || device.name == null || device.name == "") { return } @@ -145,6 +160,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() { v.setOnClickListener(this@ViewHolder) } + @SuppressLint("MissingPermission") override fun onClick(v: View) { sp.putString(R.string.key_diaconn_g8_address, item.device.address) sp.putString(R.string.key_diaconn_g8_name, name.text.toString()) @@ -153,6 +169,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() { finish() } + @SuppressLint("MissingPermission") fun setData(data: BluetoothDeviceItem) { var tTitle = data.device.name if (tTitle == null || tTitle == "") { @@ -168,6 +185,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() { // inner class BluetoothDeviceItem internal constructor(val device: BluetoothDevice) { + override fun equals(other: Any?): Boolean { if (other !is BluetoothDeviceItem) { return false diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt index a86e3ded15..727044c418 100644 --- a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/BLECommonService.kt @@ -1,9 +1,12 @@ package info.nightscout.androidaps.diaconn.service +import android.Manifest +import android.annotation.SuppressLint import android.bluetooth.* import android.content.Context -import android.content.Intent +import android.content.pm.PackageManager import android.os.SystemClock +import androidx.core.app.ActivityCompat import dagger.android.HasAndroidInjector import info.nightscout.androidaps.activities.ErrorHelperActivity import info.nightscout.androidaps.diaconn.DiaconnG8Pump @@ -12,10 +15,11 @@ import info.nightscout.androidaps.diaconn.packet.* import info.nightscout.androidaps.events.EventPumpStatusChanged import info.nightscout.androidaps.extensions.notify import info.nightscout.androidaps.extensions.waitMillis +import info.nightscout.androidaps.interfaces.ResourceHelper +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.utils.ToastUtils import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.LTag -import info.nightscout.androidaps.plugins.bus.RxBus -import info.nightscout.androidaps.interfaces.ResourceHelper import java.util.* import java.util.concurrent.ScheduledFuture import javax.inject.Inject @@ -66,6 +70,11 @@ class BLECommonService @Inject internal constructor( @Synchronized fun connect(from: String, address: String?): Boolean { + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission)) + aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from") + return false + } aapsLogger.debug(LTag.PUMPBTCOMM, "Initializing Bluetooth ") if (bluetoothAdapter == null) { aapsLogger.error("Unable to obtain a BluetoothAdapter.") @@ -98,6 +107,10 @@ class BLECommonService @Inject internal constructor( @Synchronized fun disconnect(from: String) { + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from") + return + } aapsLogger.debug(LTag.PUMPBTCOMM, "disconnect from: $from") // cancel previous scheduled disconnection to prevent closing upcoming connection @@ -117,6 +130,7 @@ class BLECommonService @Inject internal constructor( SystemClock.sleep(2000) } + @SuppressLint("MissingPermission") @Synchronized fun close() { aapsLogger.debug(LTag.PUMPBTCOMM, "BluetoothAdapter close") @@ -159,6 +173,7 @@ class BLECommonService @Inject internal constructor( } } + @SuppressLint("MissingPermission") @Synchronized private fun writeCharacteristicNoResponse(characteristic: BluetoothGattCharacteristic, data: ByteArray) { Thread(Runnable { @@ -197,6 +212,7 @@ class BLECommonService @Inject internal constructor( return bluetoothGatt?.services } + @SuppressLint("MissingPermission") @Synchronized private fun findCharacteristic() { val gattServices = getSupportedGattServices() ?: return @@ -222,6 +238,7 @@ class BLECommonService @Inject internal constructor( } } + @SuppressLint("MissingPermission") @Synchronized private fun onConnectionStateChangeSynchronized(gatt: BluetoothGatt, newState: Int) { aapsLogger.debug(LTag.PUMPBTCOMM, "onConnectionStateChange newState : $newState") diff --git a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java index 114dbdbe62..88c3f6bb4c 100644 --- a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java +++ b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/connection_service/InsightConnectionService.java @@ -1,16 +1,19 @@ package info.nightscout.androidaps.plugins.pump.insight.connection_service; +import android.Manifest; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothSocket; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.os.Binder; import android.os.IBinder; import android.os.PowerManager; import androidx.annotation.Nullable; +import androidx.core.app.ActivityCompat; import org.spongycastle.crypto.InvalidCipherTextException; @@ -23,8 +26,6 @@ import javax.inject.Inject; import dagger.android.DaggerService; import info.nightscout.androidaps.insight.R; -import info.nightscout.shared.logging.AAPSLogger; -import info.nightscout.shared.logging.LTag; import info.nightscout.androidaps.plugins.pump.insight.app_layer.AppLayerMessage; import info.nightscout.androidaps.plugins.pump.insight.app_layer.ReadParameterBlockMessage; import info.nightscout.androidaps.plugins.pump.insight.app_layer.configuration.CloseConfigurationWriteSessionMessage; @@ -87,6 +88,8 @@ import info.nightscout.androidaps.plugins.pump.insight.utils.PairingDataStorage; import info.nightscout.androidaps.plugins.pump.insight.utils.crypto.Cryptograph; import info.nightscout.androidaps.plugins.pump.insight.utils.crypto.DerivedKeys; import info.nightscout.androidaps.plugins.pump.insight.utils.crypto.KeyPair; +import info.nightscout.shared.logging.AAPSLogger; +import info.nightscout.shared.logging.LTag; import info.nightscout.shared.sharedPreferences.SP; public class InsightConnectionService extends DaggerService implements ConnectionEstablisher.Callback, InputStreamReader.Callback, OutputStreamWriter.Callback { @@ -108,7 +111,7 @@ public class InsightConnectionService extends DaggerService implements Connectio private DelayedActionThread disconnectTimer; private DelayedActionThread recoveryTimer; private DelayedActionThread timeoutTimer; - private BluetoothAdapter bluetoothAdapter; + @Nullable private BluetoothAdapter bluetoothAdapter; private BluetoothDevice bluetoothDevice; private BluetoothSocket bluetoothSocket; private ConnectionEstablisher connectionEstablisher; @@ -258,10 +261,12 @@ public class InsightConnectionService extends DaggerService implements Connectio @Override public synchronized void onCreate() { super.onCreate(); - bluetoothAdapter = ((BluetoothManager)getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); - pairingDataStorage = new PairingDataStorage(this); - state = pairingDataStorage.isPaired() ? InsightState.DISCONNECTED : InsightState.NOT_PAIRED; - wakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidAPS:InsightConnectionService"); + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { + bluetoothAdapter = ((BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); + pairingDataStorage = new PairingDataStorage(this); + state = pairingDataStorage.isPaired() ? InsightState.DISCONNECTED : InsightState.NOT_PAIRED; + wakeLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidAPS:InsightConnectionService"); + } } private void setState(InsightState state) { @@ -426,6 +431,7 @@ public class InsightConnectionService extends DaggerService implements Connectio } private synchronized void connect() { + if (bluetoothAdapter == null) return; if (bluetoothDevice == null) bluetoothDevice = bluetoothAdapter.getRemoteDevice(pairingDataStorage.getMacAddress()); setState(InsightState.CONNECTING); diff --git a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpBLEConfigActivity.kt b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpBLEConfigActivity.kt index 6333af2cc2..db19107d75 100755 --- a/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpBLEConfigActivity.kt +++ b/pump-common/src/main/java/info/nightscout/androidaps/plugins/pump/common/ui/PumpBLEConfigActivity.kt @@ -133,7 +133,7 @@ class PumpBLEConfigActivity : DaggerAppCompatActivity() { aapsLogger.debug(TAG, "Device can be detected near, so trying to remove bond if possible.") bleSelector.removeDevice(bluetoothDevice!!) } else { - val remoteDevice = bluetoothAdapter!!.getRemoteDevice(deviceAddress) + val remoteDevice = bluetoothAdapter?.getRemoteDevice(deviceAddress) if (remoteDevice != null) { bleSelector.removeDevice(remoteDevice) } @@ -184,7 +184,7 @@ class PumpBLEConfigActivity : DaggerAppCompatActivity() { } private fun prepareForScanning() { - bleScanner = bluetoothAdapter!!.bluetoothLeScanner + bleScanner = bluetoothAdapter?.bluetoothLeScanner settings = bleSelector.getScanSettings() filters = bleSelector.getScanFilters() } @@ -243,7 +243,7 @@ class PumpBLEConfigActivity : DaggerAppCompatActivity() { binding.pumpBleConfigButtonScanStop.visibility = View.VISIBLE } scanning = true - bleScanner!!.startScan(filters, settings, bleScanCallback) + bleScanner?.startScan(filters, settings, bleScanCallback) aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start") bleSelector.onStartLeDeviceScan(this@PumpBLEConfigActivity) } @@ -251,7 +251,7 @@ class PumpBLEConfigActivity : DaggerAppCompatActivity() { private fun stopLeDeviceScan(manualStop: Boolean) { if (scanning) { scanning = false - bleScanner!!.stopScan(bleScanCallback) + bleScanner?.stopScan(bleScanCallback) aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop") bleSelector.onStopLeDeviceScan(this@PumpBLEConfigActivity) handler.removeCallbacks(stopScanAfterTimeoutRunnable) @@ -320,8 +320,8 @@ class PumpBLEConfigActivity : DaggerAppCompatActivity() { if (currentlySelectedAddress == device.address) { deviceName += " (" + resources.getString(R.string.ble_config_scan_selected) + ")" } - holder.deviceName!!.text = deviceName - holder.deviceAddress!!.text = device.address + holder.deviceName?.text = deviceName + holder.deviceAddress?.text = device.address return v!! } } diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt index 61be9e9b16..e02dbaff7b 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/dialog/RileyLinkBLEConfigActivity.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.pump.common.dialog +import android.Manifest import android.annotation.SuppressLint import android.bluetooth.BluetoothAdapter import android.bluetooth.BluetoothDevice @@ -10,6 +11,7 @@ import android.bluetooth.le.ScanFilter import android.bluetooth.le.ScanResult import android.bluetooth.le.ScanSettings import android.content.Context +import android.content.pm.PackageManager import android.os.Bundle import android.os.Handler import android.os.HandlerThread @@ -22,8 +24,10 @@ import android.widget.AdapterView.OnItemClickListener import android.widget.BaseAdapter import android.widget.TextView import android.widget.Toast +import androidx.core.app.ActivityCompat import dagger.android.support.DaggerAppCompatActivity import info.nightscout.androidaps.interfaces.ActivePlugin +import info.nightscout.androidaps.interfaces.ResourceHelper import info.nightscout.androidaps.plugins.pump.common.ble.BlePreCheck import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.R import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst @@ -32,7 +36,6 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.Gatt import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.databinding.RileyLinkBleConfigActivityBinding import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.defs.RileyLinkPumpDevice import info.nightscout.androidaps.utils.alertDialogs.OKDialog -import info.nightscout.androidaps.interfaces.ResourceHelper import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.logging.LTag import info.nightscout.shared.sharedPreferences.SP @@ -225,16 +228,20 @@ class RileyLinkBLEConfigActivity : DaggerAppCompatActivity() { binding.rileyLinkBleConfigButtonScanStop.visibility = View.VISIBLE } scanning = true - bleScanner?.startScan(filters, settings, bleScanCallback) - aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start") - Toast.makeText(this@RileyLinkBLEConfigActivity, R.string.riley_link_ble_config_scan_scanning, Toast.LENGTH_SHORT).show() + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) { + bleScanner?.startScan(filters, settings, bleScanCallback) + aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start") + Toast.makeText(this@RileyLinkBLEConfigActivity, R.string.riley_link_ble_config_scan_scanning, Toast.LENGTH_SHORT).show() + } } private fun stopLeDeviceScan() { if (scanning) { scanning = false if (bluetoothAdapter?.isEnabled == true && bluetoothAdapter?.state == BluetoothAdapter.STATE_ON) - bleScanner?.stopScan(bleScanCallback) + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) { + bleScanner?.stopScan(bleScanCallback) + } aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop") Toast.makeText(this, R.string.riley_link_ble_config_scan_finished, Toast.LENGTH_SHORT).show() handler.removeCallbacks(stopScanAfterTimeoutRunnable) @@ -268,7 +275,7 @@ class RileyLinkBLEConfigActivity : DaggerAppCompatActivity() { override fun getItem(i: Int): Any = leDevices[i] override fun getItemId(i: Int): Long = i.toLong() - @SuppressLint("InflateParams") + @SuppressLint("InflateParams", "MissingPermission") override fun getView(i: Int, v: View?, viewGroup: ViewGroup): View { var view = v val viewHolder: ViewHolder