Suppress BT deprecation comming from targeting Andorid 13 (API 33)

This commit is contained in:
Milos Kozak 2022-09-21 15:26:44 +02:00
parent a1c987695b
commit 7ffe1e28f5
9 changed files with 23 additions and 33 deletions

View file

@ -213,35 +213,12 @@ class BLEComm @Inject internal constructor(
@SuppressLint("MissingPermission")
@Synchronized fun close() {
/*
if (!encryptedDataRead && !encryptedCommandSent) {
// there was no response from pump before started encryption
// assume pairing is invalid
val lastClearRequest = sp.getLong(R.string.key_rs_last_clear_key_request, 0)
if (lastClearRequest != 0L && dateUtil.isOlderThan(lastClearRequest, 5)) {
ToastUtils.showToastInUiThread(context, R.string.invalidpairing)
danaRSPlugin.changePump()
sp.getStringOrNull(R.string.key_danars_address, null)?.let { address ->
bluetoothAdapter?.getRemoteDevice(address)?.let { device ->
try {
aapsLogger.debug(LTag.PUMPBTCOMM, "Removing bond")
device::class.java.getMethod("removeBond").invoke(device)
} catch (e: Exception) {
aapsLogger.error("Removing bond has been failed. ${e.message}")
}
}
}
} else if (lastClearRequest == 0L) {
aapsLogger.error("Clearing pairing keys postponed")
sp.putLong(R.string.key_rs_last_clear_key_request, dateUtil.now())
}
}
*/
aapsLogger.debug(LTag.PUMPBTCOMM, "BluetoothAdapter close")
bluetoothGatt?.close()
bluetoothGatt = null
}
@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")
private val mGattCallback: BluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
onConnectionStateChangeSynchronized(gatt, newState) // call it synchronized
@ -289,6 +266,7 @@ class BLEComm @Inject internal constructor(
}
}
@Suppress("DEPRECATION")
@SuppressLint("MissingPermission")
@Synchronized
private fun setCharacteristicNotification(characteristic: BluetoothGattCharacteristic?, enabled: Boolean) {
@ -309,6 +287,7 @@ class BLEComm @Inject internal constructor(
}
}
@Suppress("DEPRECATION")
@SuppressLint("MissingPermission")
@Synchronized
private fun writeCharacteristicNoResponse(characteristic: BluetoothGattCharacteristic, data: ByteArray) {

View file

@ -144,6 +144,7 @@ class BLECommonService @Inject internal constructor(
bluetoothGatt = null
}
@Suppress("OVERRIDE_DEPRECATION", "DEPRECATION")
private val mGattCallback: BluetoothGattCallback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
onConnectionStateChangeSynchronized(gatt, newState) // call it synchronized
@ -179,6 +180,7 @@ class BLECommonService @Inject internal constructor(
}
}
@Suppress("DEPRECATION")
@SuppressLint("MissingPermission")
@Synchronized
private fun writeCharacteristicNoResponse(characteristic: BluetoothGattCharacteristic, data: ByteArray) {
@ -218,6 +220,7 @@ class BLECommonService @Inject internal constructor(
return bluetoothGatt?.services
}
@Suppress("DEPRECATION")
@SuppressLint("MissingPermission")
@Synchronized
private fun findCharacteristic() {

View file

@ -103,6 +103,7 @@ class BleCommCallbacks(
}
}
@Suppress("DEPRECATION")
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
aapsLogger.debug(
LTag.PUMPBTCOMM,
@ -115,6 +116,7 @@ class BleCommCallbacks(
onWrite(status, characteristic.uuid, characteristic.value)
}
@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
super.onCharacteristicChanged(gatt, characteristic)
@ -134,6 +136,7 @@ class BleCommCallbacks(
}
}
@Suppress("DEPRECATION")
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
super.onDescriptorWrite(gatt, descriptor, status)

View file

@ -24,7 +24,7 @@ data class BleSendErrorConfirming(val msg: String, val cause: Throwable? = null)
open class BleIO(
private val aapsLogger: AAPSLogger,
var characteristic: BluetoothGattCharacteristic,
private var characteristic: BluetoothGattCharacteristic,
private val incomingPackets: BlockingQueue<ByteArray>,
private val gatt: BluetoothGatt,
private val bleCommCallbacks: BleCommCallbacks,
@ -33,7 +33,6 @@ open class BleIO(
/***
*
* @param characteristic where to read from(CMD or DATA)
* @return a byte array with the received data or error
*/
fun receivePacket(timeoutMs: Long = DEFAULT_IO_TIMEOUT_MS): ByteArray? {
@ -51,10 +50,9 @@ open class BleIO(
/***
*
* @param characteristic where to write to(CMD or DATA)
* @param payload the data to send
*/
@Suppress("ReturnCount")
@Suppress("ReturnCount", "DEPRECATION")
fun sendAndConfirmPacket(payload: ByteArray): BleSendResult {
aapsLogger.debug(LTag.PUMPBTCOMM, "BleIO: Sending on $type: ${payload.toHex()}")
val set = characteristic.setValue(payload)
@ -103,7 +101,7 @@ open class BleIO(
* This will signal the pod it can start sending back data
* @return
*/
fun readyToRead(): BleSendResult {
@Suppress("DEPRECATION") fun readyToRead(): BleSendResult {
gatt.setCharacteristicNotification(characteristic, true)
.assertTrue("enable notifications")

View file

@ -324,6 +324,7 @@ class RileyLinkBLE @Inject constructor(
init {
//orangeLink.rileyLinkBLE = this;
bluetoothGattCallback = object : BluetoothGattCallback() {
@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
super.onCharacteristicChanged(gatt, characteristic)
if (gattDebugEnabled) {
@ -333,9 +334,10 @@ class RileyLinkBLE @Inject constructor(
}
if (characteristic.uuid == UUID.fromString(GattAttributes.CHARA_RADIO_RESPONSE_COUNT))
radioResponseCountNotified?.run()
orangeLink.onCharacteristicChanged(characteristic)
orangeLink.onCharacteristicChanged(characteristic, characteristic.value)
}
@Suppress("OVERRIDE_DEPRECATION", "DEPRECATION")
override fun onCharacteristicRead(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
super.onCharacteristicRead(gatt, characteristic, status)
val statusMessage = getGattStatusMessage(status)
@ -344,6 +346,7 @@ class RileyLinkBLE @Inject constructor(
mCurrentOperation?.gattOperationCompletionCallback(characteristic.uuid, characteristic.value)
}
@Suppress("DEPRECATION")
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
super.onCharacteristicWrite(gatt, characteristic, status)
val uuidString = GattAttributes.lookup(characteristic.uuid)
@ -387,6 +390,7 @@ class RileyLinkBLE @Inject constructor(
}
}
@Suppress("DEPRECATION")
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
super.onDescriptorWrite(gatt, descriptor, status)
if (gattDebugEnabled)
@ -394,6 +398,7 @@ class RileyLinkBLE @Inject constructor(
mCurrentOperation?.gattOperationCompletionCallback(descriptor.uuid, descriptor.value)
}
@Suppress("OVERRIDE_DEPRECATION", "DEPRECATION")
override fun onDescriptorRead(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
super.onDescriptorRead(gatt, descriptor, status)
mCurrentOperation?.gattOperationCompletionCallback(descriptor.uuid, descriptor.value)

View file

@ -32,11 +32,10 @@ class OrangeLinkImpl @Inject constructor(
lateinit var rileyLinkBLE: RileyLinkBLE
fun onCharacteristicChanged(characteristic: BluetoothGattCharacteristic) {
fun onCharacteristicChanged(characteristic: BluetoothGattCharacteristic, data: ByteArray) {
if (characteristic.uuid.toString() == GattAttributes.CHARA_NOTIFICATION_ORANGE) {
val data = characteristic.value
val first = 0xff and data[0].toInt()
aapsLogger.info(LTag.PUMPBTCOMM, "OrangeLinkImpl: onCharacteristicChanged ${ByteUtil.shortHexString(characteristic.value)}=====$first")
aapsLogger.info(LTag.PUMPBTCOMM, "OrangeLinkImpl: onCharacteristicChanged ${ByteUtil.shortHexString(data)}=====$first")
val fv = data[3].toString() + "." + data[4]
val hv = data[5].toString() + "." + data[6]
rileyLinkServiceData.versionOrangeFirmware = fv

View file

@ -29,6 +29,7 @@ public class CharacteristicReadOperation extends BLECommOperation {
}
@SuppressWarnings({"deprecation"})
@Override
public void execute(RileyLinkBLE comm) {
gatt.readCharacteristic(characteristic);

View file

@ -30,6 +30,7 @@ public class CharacteristicWriteOperation extends BLECommOperation {
}
@SuppressWarnings({"deprecation"})
@Override
public void execute(RileyLinkBLE comm) {

View file

@ -36,6 +36,7 @@ public class DescriptorWriteOperation extends BLECommOperation {
}
@SuppressWarnings({"deprecation"})
@Override
public void execute(RileyLinkBLE comm) {
descr.setValue(value);