From b1d45833c5570910142e78ba39a8322b4039a9d5 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 13 Feb 2022 22:13:02 +0100 Subject: [PATCH] RL: check for permission before connect --- .../common/hw/rileylink/ble/RileyLinkBLE.kt | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.kt b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.kt index ef8d8bb34e..ace3cdb3d1 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.kt +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/RileyLinkBLE.kt @@ -3,7 +3,11 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble import android.annotation.SuppressLint import android.bluetooth.* import android.content.Context +import android.content.pm.PackageManager +import android.os.Build import android.os.SystemClock +import androidx.core.content.ContextCompat +import info.nightscout.androidaps.interfaces.Config import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkConst import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.GattAttributes @@ -32,14 +36,15 @@ import javax.inject.Singleton * Added: State handling, configuration of RF for different configuration ranges, connection handling */ @Singleton -class RileyLinkBLE @Inject constructor(private val context: Context) { - - @Inject lateinit var aapsLogger: AAPSLogger - @Inject lateinit var rileyLinkServiceData: RileyLinkServiceData - @Inject lateinit var rileyLinkUtil: RileyLinkUtil - @Inject lateinit var sp: SP - @Inject lateinit var orangeLink: OrangeLinkImpl - +class RileyLinkBLE @Inject constructor( + private val context: Context, + private val aapsLogger: AAPSLogger, + private val rileyLinkServiceData: RileyLinkServiceData, + private val rileyLinkUtil: RileyLinkUtil, + private val sp: SP, + private val orangeLink: OrangeLinkImpl, + private val config: Config +) { private val gattDebugEnabled = true private var manualDisconnect = false @@ -154,7 +159,12 @@ class RileyLinkBLE @Inject constructor(private val context: Context) { aapsLogger.error(LTag.PUMPBTCOMM, "RileyLink device is null, can't do connectGatt.") return } - bluetoothConnectionGatt = rileyLinkDevice?.connectGatt(context, true, bluetoothGattCallback) + if (config.PUMPDRIVERS && Build.VERSION.SDK_INT >= /*Build.VERSION_CODES.S*/31 && + ContextCompat.checkSelfPermission(context, "android.permission.BLUETOOTH_CONNECT") == PackageManager.PERMISSION_GRANTED + ) { + aapsLogger.debug(LTag.PUMPBTCOMM, "no permission") + return + } else bluetoothConnectionGatt = rileyLinkDevice?.connectGatt(context, true, bluetoothGattCallback) // , BluetoothDevice.TRANSPORT_LE if (bluetoothConnectionGatt == null) aapsLogger.error(LTag.PUMPBTCOMM, "Failed to connect to Bluetooth Low Energy device at " + bluetoothAdapter?.address)