From 3ffb78a7e85dfa111e864e3a61c1acd5d9fa44b7 Mon Sep 17 00:00:00 2001 From: jbr7rr <> Date: Fri, 23 Jun 2023 11:42:45 +0200 Subject: [PATCH] Fix ANR, optimize write delay --- .../info/nightscout/pump/medtrum/services/BLEComm.kt | 10 ++++++---- .../nightscout/pump/medtrum/services/MedtrumService.kt | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt index 8707aa1309..beba3622d0 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/BLEComm.kt @@ -72,7 +72,7 @@ class BLEComm @Inject internal constructor( companion object { - private const val WRITE_DELAY_MILLIS: Long = 100 + private const val WRITE_DELAY_MILLIS: Long = 10 private const val SERVICE_UUID = "669A9001-0008-968F-E311-6050405558B3" private const val READ_UUID = "669a9120-0008-968f-e311-6050405558b3" private const val WRITE_UUID = "669a9101-0008-968f-e311-6050405558b3" @@ -455,11 +455,13 @@ class BLEComm @Inject internal constructor( } else { characteristic.value = data characteristic.writeType = BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT - aapsLogger.debug(LTag.PUMPBTCOMM, "writeCharacteristic:" + Arrays.toString(data)) - mBluetoothGatt?.writeCharacteristic(characteristic) + aapsLogger.debug(LTag.PUMPBTCOMM, "writeCharacteristic: ${Arrays.toString(data)}") + val success = mBluetoothGatt?.writeCharacteristic(characteristic) + if (success != true) { + mCallback?.onSendMessageError("Failed to write characteristic") + } } }, WRITE_DELAY_MILLIS) - SystemClock.sleep(WRITE_DELAY_MILLIS) } private val uartWriteBTGattChar: BluetoothGattCharacteristic diff --git a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt index 60d6b5ba8f..9985c85e48 100644 --- a/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt +++ b/pump/medtrum/src/main/java/info/nightscout/pump/medtrum/services/MedtrumService.kt @@ -92,7 +92,7 @@ class MedtrumService : DaggerService(), BLECommCallback { private var currentState: State = IdleState() private var mPacket: MedtrumPacket? = null - private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) + private val scope = CoroutineScope(Dispatchers.Default) val isConnected: Boolean get() = medtrumPump.connectionState == ConnectionState.CONNECTED