BLEComm: Synchronize read and writes better

This commit is contained in:
jbr7rr 2023-06-20 12:00:04 +02:00
parent 2302711cdf
commit bc8876b6dd

View file

@ -99,6 +99,7 @@ class BLEComm @Inject internal constructor(
private var mWritePackets: WriteCommandPackets? = null private var mWritePackets: WriteCommandPackets? = null
private var mWriteSequenceNumber: Int = 0 private var mWriteSequenceNumber: Int = 0
private var mReadPacket: ReadDataPacket? = null private var mReadPacket: ReadDataPacket? = null
private val readLock = Any()
private var mDeviceSN: Long = 0 private var mDeviceSN: Long = 0
private var mCallback: BLECommCallback? = null private var mCallback: BLECommCallback? = null
@ -268,6 +269,7 @@ class BLEComm @Inject internal constructor(
if (characteristic.getUuid() == UUID.fromString(READ_UUID)) { if (characteristic.getUuid() == UUID.fromString(READ_UUID)) {
mCallback?.onNotification(value) mCallback?.onNotification(value)
} else if (characteristic.getUuid() == UUID.fromString(WRITE_UUID)) { } else if (characteristic.getUuid() == UUID.fromString(WRITE_UUID)) {
synchronized(readLock) {
if (mReadPacket == null) { if (mReadPacket == null) {
mReadPacket = ReadDataPacket(value) mReadPacket = ReadDataPacket(value)
} else { } else {
@ -279,6 +281,7 @@ class BLEComm @Inject internal constructor(
} }
} }
} }
}
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) { override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicWrite status = " + status) aapsLogger.debug(LTag.PUMPBTCOMM, "onCharacteristicWrite status = " + status)
@ -456,6 +459,7 @@ class BLEComm @Inject internal constructor(
mBluetoothGatt?.writeCharacteristic(characteristic) mBluetoothGatt?.writeCharacteristic(characteristic)
} }
}, WRITE_DELAY_MILLIS) }, WRITE_DELAY_MILLIS)
SystemClock.sleep(WRITE_DELAY_MILLIS)
} }
private val uartWriteBTGattChar: BluetoothGattCharacteristic private val uartWriteBTGattChar: BluetoothGattCharacteristic