-
-
Save CristianMG/9eae9dafbf6ef74594e0561fccadfb5a to your computer and use it in GitHub Desktop.
Bluetooth write
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
suspend 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.chunkAndEach(mtuSize - 3) { | |
if(it.size == (mtuSize - 3)) { | |
write(characteristic, it, WriteType.WithoutResponse) | |
} else { | |
write(characteristic, it, WriteType.WithResponse) | |
} | |
} | |
} else { | |
Napier.d("data size is smaller than maxWriteSize,write it directly") | |
write(characteristic, data, 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 | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment