Cristian MG
07/13/2023, 12:02 PMtravis
07/15/2023, 7:32 AMtravis
07/15/2023, 7:35 AMThe provider of hardware tell me that the split must be done at the bluetooth socket with the mtu size, but I can’t any library provide support to this.If you’re asking why libraries don’t provide support for chunking out-of-the-box, it is because on the peripheral side your firmware needs to understand how to re-assembled chunked messages. You essentially need to make sure the chunking and reassembling of the messages uses the same process on both sides; I’m not aware of a standard for this that a library could implement (and reliably count on the peripheral also supporting).
Cristian MG
07/17/2023, 10:07 AMCristian MG
07/17/2023, 10:08 AMtravis
07/17/2023, 5:32 PMCristian MG
09/07/2023, 6:28 AMCristian MG
09/07/2023, 6:29 AMCristian MG
09/07/2023, 6:30 AMsuspend fun Peripheral.write(
characteristic: Characteristic,
data: ByteArray,
mtuSize: Int
): ByteArray =
observe(characteristic) {
if (data.size > (mtuSize - 3)) {
Napier.d("data size is bigger than mtu, chunk it and write it")
data.plus(0.toByte())
.chunkAndEach(mtuSize - 3) {
write(characteristic, it, WriteType.WithResponse)
}
} else {
Napier.d("data size is smaller than maxWriteSize,write it directly")
write(characteristic, data.plus(0.toByte()), WriteType.WithResponse)
}
}
.transformWhile {
Napier.d(
"takeWhile: size:${it.size}" + " Cadena: " + it.decodeToString(
throwOnInvalidSequence = true
)
)
emit(it)
!it.isMtuSizeReached(mtuSize)
}.fold(byteArrayOf()) { initial, value ->
val concat = initial.plus(value)
concat
}
Cristian MG
09/07/2023, 6:30 AMtravis
09/07/2023, 5:15 PM