fix BT check for android < 12

This commit is contained in:
Milos Kozak 2022-07-22 19:16:00 +02:00
parent dc13682d5e
commit aa54e94c35
10 changed files with 53 additions and 32 deletions

View file

@ -5,6 +5,7 @@ import android.bluetooth.BluetoothManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings import android.provider.Settings
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
@ -113,7 +114,7 @@ class ImportExportPrefsImpl @Inject constructor(
val n1 = Settings.System.getString(context.contentResolver, "bluetooth_name") val n1 = Settings.System.getString(context.contentResolver, "bluetooth_name")
val n2 = Settings.Secure.getString(context.contentResolver, "bluetooth_name") val n2 = Settings.Secure.getString(context.contentResolver, "bluetooth_name")
val n3 = try { val n3 = try {
if (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) {
(context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.name (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.name
} else null } else null
} catch (e: Exception){ } catch (e: Exception){

View file

@ -4,6 +4,7 @@ import android.Manifest
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import com.google.common.base.Optional import com.google.common.base.Optional
@ -77,7 +78,7 @@ class TriggerBTDevice(injector: HasAndroidInjector) : Trigger(injector) {
// Get the list of paired BT devices to use in dropdown menu // Get the list of paired BT devices to use in dropdown menu
private fun devicesPaired(): ArrayList<CharSequence> { private fun devicesPaired(): ArrayList<CharSequence> {
val s = ArrayList<CharSequence>() val s = ArrayList<CharSequence>()
if (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) {
(context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.bondedDevices?.forEach { s.add(it.name) } (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.bondedDevices?.forEach { s.add(it.name) }
} else { } else {
ToastUtils.errorToast(context, context.getString(R.string.needconnectpermission)) ToastUtils.errorToast(context, context.getString(R.string.needconnectpermission))

View file

@ -5,6 +5,7 @@ import kotlin.jvm.JvmOverloads
import android.bluetooth.BluetoothManager import android.bluetooth.BluetoothManager
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import android.util.AttributeSet import android.util.AttributeSet
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.preference.ListPreference import androidx.preference.ListPreference
@ -15,7 +16,7 @@ import java.util.*
class BluetoothDevicePreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ListPreference(context, attrs) { class BluetoothDevicePreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : ListPreference(context, attrs) {
init { init {
if (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) {
val devices = Vector<CharSequence>() val devices = Vector<CharSequence>()
(context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bta -> (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bta ->
for (dev in bta.bondedDevices) for (dev in bta.bondedDevices)

View file

@ -8,6 +8,7 @@ import android.bluetooth.BluetoothSocket;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
@ -195,7 +196,7 @@ public abstract class AbstractDanaRExecutionService extends DaggerService {
protected void getBTSocketForSelectedPump() { protected void getBTSocketForSelectedPump() {
mDevName = sp.getString(R.string.key_danar_bt_name, ""); mDevName = sp.getString(R.string.key_danar_bt_name, "");
if (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) {
BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); BluetoothAdapter bluetoothAdapter = ((BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
if (bluetoothAdapter != null) { if (bluetoothAdapter != null) {

View file

@ -11,6 +11,7 @@ import android.bluetooth.le.ScanResult
import android.content.Context import android.content.Context
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
@ -60,7 +61,7 @@ class BLEScanActivity : NoSplashAppCompatActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (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() if (bluetoothAdapter?.isEnabled != true) bluetoothAdapter?.enable()
startScan() startScan()
} else { } else {
@ -74,7 +75,7 @@ class BLEScanActivity : NoSplashAppCompatActivity() {
} }
private fun startScan() = private fun startScan() =
if (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) {
try { try {
bluetoothLeScanner?.startScan(mBleScanCallback) bluetoothLeScanner?.startScan(mBleScanCallback)
} catch (ignore: IllegalStateException) { } catch (ignore: IllegalStateException) {
@ -84,7 +85,7 @@ class BLEScanActivity : NoSplashAppCompatActivity() {
} }
private fun stopScan() = private fun stopScan() =
if (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) {
try { try {
bluetoothLeScanner?.stopScan(mBleScanCallback) bluetoothLeScanner?.stopScan(mBleScanCallback)
} catch (ignore: IllegalStateException) { } catch (ignore: IllegalStateException) {
@ -147,7 +148,7 @@ class BLEScanActivity : NoSplashAppCompatActivity() {
override fun onClick(v: View) { override fun onClick(v: View) {
sp.putString(R.string.key_danars_address, item.device.address) sp.putString(R.string.key_danars_address, item.device.address)
sp.putString(R.string.key_danars_name, name.text.toString()) sp.putString(R.string.key_danars_name, name.text.toString())
if (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) {
item.device.createBond() item.device.createBond()
rxBus.send(EventDanaRSDeviceChange()) rxBus.send(EventDanaRSDeviceChange())
} else { } else {

View file

@ -6,6 +6,7 @@ import android.bluetooth.*
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import android.os.SystemClock import android.os.SystemClock
import android.util.Base64 import android.util.Base64
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
@ -94,7 +95,9 @@ class BLEComm @Inject internal constructor(
@Synchronized @Synchronized
fun connect(from: String, address: String?): Boolean { fun connect(from: String, address: String?): Boolean {
if (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
) {
ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission)) ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission))
aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from") aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from")
return false return false
@ -116,7 +119,7 @@ class BLEComm @Inject internal constructor(
return false return false
} }
if (device.bondState == BluetoothDevice.BOND_NONE) { if (device.bondState == BluetoothDevice.BOND_NONE) {
if (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) {
device.createBond() device.createBond()
SystemClock.sleep(10000) SystemClock.sleep(10000)
} }
@ -142,7 +145,9 @@ class BLEComm @Inject internal constructor(
@Synchronized @Synchronized
fun disconnect(from: String) { fun disconnect(from: String) {
if (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
) {
aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from") aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from")
return return
} }

View file

@ -13,6 +13,7 @@ import android.bluetooth.le.ScanSettings
import android.content.Context import android.content.Context
import android.content.pm.ActivityInfo import android.content.pm.ActivityInfo
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
@ -64,7 +65,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (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() if (!bluetoothAdapter.isEnabled) bluetoothAdapter.enable()
bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
@ -77,7 +78,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
if (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) {
stopScan() stopScan()
} }
} }
@ -107,7 +108,9 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() {
} // ignore BT not on } // ignore BT not on
private fun addBleDevice(device: BluetoothDevice?) { private fun addBleDevice(device: BluetoothDevice?) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED
) {
ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission)) ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission))
return return
} }

View file

@ -5,6 +5,7 @@ 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.content.pm.PackageManager
import android.os.Build
import android.os.SystemClock import android.os.SystemClock
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
@ -38,6 +39,7 @@ class BLECommonService @Inject internal constructor(
) { ) {
companion object { companion object {
private const val WRITE_DELAY_MILLIS: Long = 50 private const val WRITE_DELAY_MILLIS: Long = 50
private const val INDICATION_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e" private const val INDICATION_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
private const val WRITE_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e" private const val WRITE_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
@ -70,7 +72,9 @@ class BLECommonService @Inject internal constructor(
@Synchronized @Synchronized
fun connect(from: String, address: String?): Boolean { fun connect(from: String, address: String?): Boolean {
if (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
) {
ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission)) ToastUtils.errorToast(context, context.getString(info.nightscout.androidaps.core.R.string.needconnectpermission))
aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from") aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from")
return false return false
@ -107,7 +111,9 @@ class BLECommonService @Inject internal constructor(
@Synchronized @Synchronized
fun disconnect(from: String) { fun disconnect(from: String) {
if (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
) {
aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from") aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from")
return return
} }

View file

@ -9,6 +9,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Binder; import android.os.Binder;
import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
@ -261,7 +262,7 @@ public class InsightConnectionService extends DaggerService implements Connectio
@Override @Override
public synchronized void onCreate() { public synchronized void onCreate() {
super.onCreate(); super.onCreate();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
bluetoothAdapter = ((BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter(); bluetoothAdapter = ((BluetoothManager) getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
pairingDataStorage = new PairingDataStorage(this); pairingDataStorage = new PairingDataStorage(this);
state = pairingDataStorage.isPaired() ? InsightState.DISCONNECTED : InsightState.NOT_PAIRED; state = pairingDataStorage.isPaired() ? InsightState.DISCONNECTED : InsightState.NOT_PAIRED;

View file

@ -12,6 +12,7 @@ import android.bluetooth.le.ScanResult
import android.bluetooth.le.ScanSettings import android.bluetooth.le.ScanSettings
import android.content.Context import android.content.Context
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.HandlerThread import android.os.HandlerThread
@ -228,7 +229,7 @@ class RileyLinkBLEConfigActivity : DaggerAppCompatActivity() {
binding.rileyLinkBleConfigButtonScanStop.visibility = View.VISIBLE binding.rileyLinkBleConfigButtonScanStop.visibility = View.VISIBLE
} }
scanning = true scanning = true
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) {
bleScanner?.startScan(filters, settings, bleScanCallback) bleScanner?.startScan(filters, settings, bleScanCallback)
aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start") aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start")
Toast.makeText(this@RileyLinkBLEConfigActivity, R.string.riley_link_ble_config_scan_scanning, Toast.LENGTH_SHORT).show() Toast.makeText(this@RileyLinkBLEConfigActivity, R.string.riley_link_ble_config_scan_scanning, Toast.LENGTH_SHORT).show()
@ -239,7 +240,7 @@ class RileyLinkBLEConfigActivity : DaggerAppCompatActivity() {
if (scanning) { if (scanning) {
scanning = false scanning = false
if (bluetoothAdapter?.isEnabled == true && bluetoothAdapter?.state == BluetoothAdapter.STATE_ON) if (bluetoothAdapter?.isEnabled == true && bluetoothAdapter?.state == BluetoothAdapter.STATE_ON)
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED) {
bleScanner?.stopScan(bleScanCallback) bleScanner?.stopScan(bleScanCallback)
} }
aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop") aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop")