RL: check for permission before connect

This commit is contained in:
Milos Kozak 2022-02-13 22:13:02 +01:00
parent 37f7049a9b
commit b1d45833c5

View file

@ -3,7 +3,11 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.bluetooth.* import android.bluetooth.*
import android.content.Context import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.SystemClock 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.RileyLinkConst
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.RileyLinkUtil
import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.data.GattAttributes 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 * Added: State handling, configuration of RF for different configuration ranges, connection handling
*/ */
@Singleton @Singleton
class RileyLinkBLE @Inject constructor(private val context: Context) { class RileyLinkBLE @Inject constructor(
private val context: Context,
@Inject lateinit var aapsLogger: AAPSLogger private val aapsLogger: AAPSLogger,
@Inject lateinit var rileyLinkServiceData: RileyLinkServiceData private val rileyLinkServiceData: RileyLinkServiceData,
@Inject lateinit var rileyLinkUtil: RileyLinkUtil private val rileyLinkUtil: RileyLinkUtil,
@Inject lateinit var sp: SP private val sp: SP,
@Inject lateinit var orangeLink: OrangeLinkImpl private val orangeLink: OrangeLinkImpl,
private val config: Config
) {
private val gattDebugEnabled = true private val gattDebugEnabled = true
private var manualDisconnect = false 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.") aapsLogger.error(LTag.PUMPBTCOMM, "RileyLink device is null, can't do connectGatt.")
return 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 // , BluetoothDevice.TRANSPORT_LE
if (bluetoothConnectionGatt == null) if (bluetoothConnectionGatt == null)
aapsLogger.error(LTag.PUMPBTCOMM, "Failed to connect to Bluetooth Low Energy device at " + bluetoothAdapter?.address) aapsLogger.error(LTag.PUMPBTCOMM, "Failed to connect to Bluetooth Low Energy device at " + bluetoothAdapter?.address)