dash ble: fix reconnection and improve logging for BleCallbacks
When reconnecting, we have to use the discovered Characteristics and not the ones that we used for the previous connection
This commit is contained in:
parent
a29874dc6d
commit
9597922058
3 changed files with 48 additions and 8 deletions
|
@ -26,16 +26,16 @@ class BleCommCallbacks(
|
||||||
private val writeQueue: BlockingQueue<WriteConfirmation> = LinkedBlockingQueue(1)
|
private val writeQueue: BlockingQueue<WriteConfirmation> = LinkedBlockingQueue(1)
|
||||||
|
|
||||||
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
|
||||||
super.onConnectionStateChange(gatt, status, newState)
|
|
||||||
aapsLogger.debug(LTag.PUMPBTCOMM, "OnConnectionStateChange with status/state: $status/$newState")
|
aapsLogger.debug(LTag.PUMPBTCOMM, "OnConnectionStateChange with status/state: $status/$newState")
|
||||||
|
super.onConnectionStateChange(gatt, status, newState)
|
||||||
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
|
if (newState == BluetoothProfile.STATE_CONNECTED && status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
connected.countDown()
|
connected.countDown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
|
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
|
||||||
|
aapsLogger.debug(LTag.PUMPBTCOMM, "OnServicesDiscovered with status: $status")
|
||||||
super.onServicesDiscovered(gatt, status)
|
super.onServicesDiscovered(gatt, status)
|
||||||
aapsLogger.debug(LTag.PUMPBTCOMM, "OnServicesDiscovered with status$status")
|
|
||||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||||
serviceDiscoveryComplete.countDown()
|
serviceDiscoveryComplete.countDown()
|
||||||
}
|
}
|
||||||
|
@ -83,14 +83,23 @@ class BleCommCallbacks(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
|
override fun onCharacteristicWrite(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int) {
|
||||||
|
aapsLogger.debug(
|
||||||
|
LTag.PUMPBTCOMM,
|
||||||
|
"OnCharacteristicWrite with char/status " +
|
||||||
|
"${characteristic.uuid} /" +
|
||||||
|
"$status"
|
||||||
|
)
|
||||||
super.onCharacteristicWrite(gatt, characteristic, status)
|
super.onCharacteristicWrite(gatt, characteristic, status)
|
||||||
|
|
||||||
onWrite(status, characteristic.uuid, characteristic.value)
|
onWrite(status, characteristic.uuid, characteristic.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
|
override fun onCharacteristicChanged(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) {
|
||||||
super.onCharacteristicChanged(gatt, characteristic)
|
super.onCharacteristicChanged(gatt, characteristic)
|
||||||
|
|
||||||
val payload = characteristic.value
|
val payload = characteristic.value
|
||||||
val characteristicType = byValue(characteristic.uuid.toString())
|
val characteristicType = byValue(characteristic.uuid.toString())
|
||||||
|
|
||||||
aapsLogger.debug(
|
aapsLogger.debug(
|
||||||
LTag.PUMPBTCOMM,
|
LTag.PUMPBTCOMM,
|
||||||
"OnCharacteristicChanged with char/value " +
|
"OnCharacteristicChanged with char/value " +
|
||||||
|
@ -107,16 +116,44 @@ class BleCommCallbacks(
|
||||||
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
|
override fun onDescriptorWrite(gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int) {
|
||||||
super.onDescriptorWrite(gatt, descriptor, status)
|
super.onDescriptorWrite(gatt, descriptor, status)
|
||||||
|
|
||||||
|
aapsLogger.debug(
|
||||||
|
LTag.PUMPBTCOMM,
|
||||||
|
"OnDescriptorWrite with descriptor/status " +
|
||||||
|
descriptor.uuid.toString() + "/" +
|
||||||
|
status + "/"
|
||||||
|
)
|
||||||
|
|
||||||
onWrite(status, descriptor.uuid, descriptor.value)
|
onWrite(status, descriptor.uuid, descriptor.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onWrite(status: Int, uuid: UUID?, value: ByteArray?) {
|
override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) {
|
||||||
if (uuid == null || value == null) {
|
super.onMtuChanged(gatt, mtu, status)
|
||||||
return
|
aapsLogger.debug(
|
||||||
|
LTag.PUMPBTCOMM,
|
||||||
|
"onMtuChanged with MTU/status: $mtu/$status "
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onReadRemoteRssi(gatt: BluetoothGatt?, rssi: Int, status: Int) {
|
||||||
|
super.onReadRemoteRssi(gatt, rssi, status)
|
||||||
|
aapsLogger.debug(
|
||||||
|
LTag.PUMPBTCOMM,
|
||||||
|
"onReadRemoteRssi with rssi/status: $rssi/$status "
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPhyUpdate(gatt: BluetoothGatt?, txPhy: Int, rxPhy: Int, status: Int) {
|
||||||
|
super.onPhyUpdate(gatt, txPhy, rxPhy, status)
|
||||||
|
aapsLogger.debug(
|
||||||
|
LTag.PUMPBTCOMM,
|
||||||
|
"onPhyUpdate with txPhy/rxPhy/status: $txPhy/$rxPhy/$status "
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun onWrite(status: Int, uuid: UUID?, value: ByteArray?) {
|
||||||
val writeConfirmation = when {
|
val writeConfirmation = when {
|
||||||
uuid == null || value == null ->
|
uuid == null || value == null ->
|
||||||
WriteConfirmationError("onWrite received Null: UUID=$uuid, value=${value.toHex()} status=$status")
|
WriteConfirmationError("onWrite received Null: UUID=$uuid, value=${value?.toHex()} status=$status")
|
||||||
|
|
||||||
status == BluetoothGatt.GATT_SUCCESS -> {
|
status == BluetoothGatt.GATT_SUCCESS -> {
|
||||||
aapsLogger.debug(LTag.PUMPBTCOMM, "OnWrite value " + value.toHex())
|
aapsLogger.debug(LTag.PUMPBTCOMM, "OnWrite value " + value.toHex())
|
||||||
|
|
|
@ -23,7 +23,7 @@ data class BleSendErrorConfirming(val msg: String, val cause: Throwable? = null)
|
||||||
|
|
||||||
open class BleIO(
|
open class BleIO(
|
||||||
private val aapsLogger: AAPSLogger,
|
private val aapsLogger: AAPSLogger,
|
||||||
private val characteristic: BluetoothGattCharacteristic,
|
var characteristic: BluetoothGattCharacteristic,
|
||||||
private val incomingPackets: BlockingQueue<ByteArray>,
|
private val incomingPackets: BlockingQueue<ByteArray>,
|
||||||
private val gatt: BluetoothGatt,
|
private val gatt: BluetoothGatt,
|
||||||
private val bleCommCallbacks: BleCommCallbacks,
|
private val bleCommCallbacks: BleCommCallbacks,
|
||||||
|
|
|
@ -88,7 +88,10 @@ class Connection(val podDevice: BluetoothDevice, private val aapsLogger: AAPSLog
|
||||||
if (waitForConnection() is NotConnected) {
|
if (waitForConnection() is NotConnected) {
|
||||||
throw FailedToConnectException(podDevice.address)
|
throw FailedToConnectException(podDevice.address)
|
||||||
}
|
}
|
||||||
discoverer.discoverServices()
|
val discovered = discoverer.discoverServices()
|
||||||
|
dataBleIO.characteristic = discovered[CharacteristicType.DATA]!!
|
||||||
|
cmdBleIO.characteristic = discovered[CharacteristicType.CMD]!!
|
||||||
|
|
||||||
cmdBleIO.hello()
|
cmdBleIO.hello()
|
||||||
cmdBleIO.readyToRead()
|
cmdBleIO.readyToRead()
|
||||||
dataBleIO.readyToRead()
|
dataBleIO.readyToRead()
|
||||||
|
|
Loading…
Reference in a new issue