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.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
@ -113,7 +114,7 @@ class ImportExportPrefsImpl @Inject constructor(
val n1 = Settings.System.getString(context.contentResolver, "bluetooth_name")
val n2 = Settings.Secure.getString(context.contentResolver, "bluetooth_name")
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
} else null
} catch (e: Exception){

View file

@ -4,6 +4,7 @@ import android.Manifest
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.widget.LinearLayout
import androidx.core.app.ActivityCompat
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
private fun devicesPaired(): 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) }
} else {
ToastUtils.errorToast(context, context.getString(R.string.needconnectpermission))

View file

@ -5,6 +5,7 @@ import kotlin.jvm.JvmOverloads
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.util.AttributeSet
import androidx.core.app.ActivityCompat
import androidx.preference.ListPreference
@ -15,7 +16,7 @@ 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) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S || ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED) {
val devices = Vector<CharSequence>()
(context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?)?.adapter?.let { bta ->
for (dev in bta.bondedDevices)

View file

@ -8,6 +8,7 @@ import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
@ -195,7 +196,7 @@ public abstract class AbstractDanaRExecutionService extends DaggerService {
protected void getBTSocketForSelectedPump() {
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();
if (bluetoothAdapter != null) {

View file

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

View file

@ -6,6 +6,7 @@ import android.bluetooth.*
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.SystemClock
import android.util.Base64
import androidx.core.app.ActivityCompat
@ -94,7 +95,9 @@ class BLEComm @Inject internal constructor(
@Synchronized
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))
aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from")
return false
@ -116,7 +119,7 @@ class BLEComm @Inject internal constructor(
return false
}
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()
SystemClock.sleep(10000)
}
@ -142,7 +145,9 @@ class BLEComm @Inject internal constructor(
@Synchronized
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")
return
}

View file

@ -13,6 +13,7 @@ import android.bluetooth.le.ScanSettings
import android.content.Context
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
@ -64,7 +65,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() {
override fun 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 ->
if (!bluetoothAdapter.isEnabled) bluetoothAdapter.enable()
bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
@ -77,7 +78,7 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() {
override fun 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()
}
}
@ -107,7 +108,9 @@ class DiaconnG8BLEScanActivity : NoSplashAppCompatActivity() {
} // ignore BT not on
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))
return
}

View file

@ -5,6 +5,7 @@ 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.app.ActivityCompat
import dagger.android.HasAndroidInjector
@ -35,9 +36,10 @@ class BLECommonService @Inject internal constructor(
private val diaconnG8ResponseMessageHashTable: DiaconnG8ResponseMessageHashTable,
private val diaconnG8SettingResponseMessageHashTable: DiaconnG8SettingResponseMessageHashTable,
private val diaconnG8Pump: DiaconnG8Pump,
){
) {
companion object {
private const val WRITE_DELAY_MILLIS: Long = 50
private const val INDICATION_UUID = "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
private const val WRITE_UUID = "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
@ -57,7 +59,7 @@ class BLECommonService @Inject internal constructor(
private var uartIndicate: BluetoothGattCharacteristic? = null
private var uartWrite: BluetoothGattCharacteristic? = null
private var mSequence : Int = 0
private var mSequence: Int = 0
private fun getMsgSequence(): Int {
val seq = mSequence % 255
@ -70,7 +72,9 @@ class BLECommonService @Inject internal constructor(
@Synchronized
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))
aapsLogger.error(LTag.PUMPBTCOMM, "missing permission: $from")
return false
@ -107,7 +111,9 @@ class BLECommonService @Inject internal constructor(
@Synchronized
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")
return
}
@ -156,7 +162,7 @@ class BLECommonService @Inject internal constructor(
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
aapsLogger.debug(LTag.PUMPBTCOMM, "(응답) onCharacteristicChanged: " + DiaconnG8Packet.toHex(characteristic.value))
// 대량로그응답 처리.
if(characteristic.value[1] == 0xb2.toByte()) {
if (characteristic.value[1] == 0xb2.toByte()) {
aapsLogger.debug(LTag.PUMPBTCOMM, "(대량 로그 처리 응답) onCharacteristicChanged: " + DiaconnG8Packet.toHex(characteristic.value))
val message = BigLogInquireResponsePacket(injector)
message.handleMessage(characteristic.value)
@ -269,7 +275,7 @@ class BLECommonService @Inject internal constructor(
writeCharacteristicNoResponse(uartWriteBTGattChar, bytes)
// 요청 큐에 요청할 바이트 정보 담기.
synchronized(mSendQueue) {
if(mSendQueue.size > 10) mSendQueue.clear()
if (mSendQueue.size > 10) mSendQueue.clear()
mSendQueue.add(bytes)
}
aapsLogger.debug(LTag.PUMPBTCOMM, "sendMessage() after mSendQueue.size :: ${mSendQueue.size}")
@ -301,29 +307,29 @@ class BLECommonService @Inject internal constructor(
aapsLogger.debug(LTag.PUMPBTCOMM, "receivedCommand :: $receivedCommand")
// 응답메시지가 조회응답인지. 설정응답인지 구분해야됨.
var message:DiaconnG8Packet? = null
var message: DiaconnG8Packet? = null
// 요청시퀀스와 응답의 시퀀스가 동일한 경우에만 처리.
// 펌프로부터 받은 보고응답의 경우 처리
if(receivedType == 3) {
if (receivedType == 3) {
message = diaconnG8ResponseMessageHashTable.findMessage(receivedCommand)
// injection Blocked Report
if(message is InjectionBlockReportPacket ) {
if (message is InjectionBlockReportPacket) {
message.handleMessage(data)
diaconnG8Pump.bolusBlocked = true
ErrorHelperActivity.runAlarm(context, rh.gs(R.string.injectionblocked), rh.gs(R.string.injectionblocked), R.raw.boluserror )
ErrorHelperActivity.runAlarm(context, rh.gs(R.string.injectionblocked), rh.gs(R.string.injectionblocked), R.raw.boluserror)
return
}
// battery warning report
if(message is BatteryWarningReportPacket ) {
if (message is BatteryWarningReportPacket) {
message.handleMessage(data)
ErrorHelperActivity.runAlarm(context, rh.gs(R.string.needbatteryreplace), rh.gs(R.string.batterywarning), R.raw.boluserror )
ErrorHelperActivity.runAlarm(context, rh.gs(R.string.needbatteryreplace), rh.gs(R.string.batterywarning), R.raw.boluserror)
return
}
// insulin lack warning report
if(message is InsulinLackReportPacket ) {
if (message is InsulinLackReportPacket) {
message.handleMessage(data)
ErrorHelperActivity.runAlarm(context, rh.gs(R.string.needinsullinreplace), rh.gs(R.string.insulinlackwarning), R.raw.boluserror )
ErrorHelperActivity.runAlarm(context, rh.gs(R.string.needinsullinreplace), rh.gs(R.string.insulinlackwarning), R.raw.boluserror)
return
}
@ -332,7 +338,7 @@ class BLECommonService @Inject internal constructor(
synchronized(mSendQueue) {
val sendQueueSize = mSendQueue.size
if (sendQueueSize > 0) {
for (i in sendQueueSize-1 downTo 0 ) {
for (i in sendQueueSize - 1 downTo 0) {
val sendQueueSeq = DiaconnG8Packet(injector).getSeq(mSendQueue[i])
val sendQueueType = DiaconnG8Packet(injector).getType(mSendQueue[i])
if (sendQueueSeq == receivedSeq) {

View file

@ -9,6 +9,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.PowerManager;
@ -261,7 +262,7 @@ public class InsightConnectionService extends DaggerService implements Connectio
@Override
public synchronized void 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();
pairingDataStorage = new PairingDataStorage(this);
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.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.HandlerThread
@ -228,7 +229,7 @@ class RileyLinkBLEConfigActivity : DaggerAppCompatActivity() {
binding.rileyLinkBleConfigButtonScanStop.visibility = View.VISIBLE
}
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)
aapsLogger.debug(LTag.PUMPBTCOMM, "startLeDeviceScan: Scanning Start")
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) {
scanning = false
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)
}
aapsLogger.debug(LTag.PUMPBTCOMM, "stopLeDeviceScan: Scanning Stop")