From 9756c5c5e3e020439cac4ecf323ff9029258874a Mon Sep 17 00:00:00 2001 From: geeksville Date: Sun, 16 Feb 2020 13:33:29 -0800 Subject: [PATCH] make sending packets to the BT radio async, for better throughput --- .../mesh/service/RadioInterfaceService.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt index 09e8834f8..7a8fb862b 100644 --- a/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt +++ b/app/src/main/java/com/geeksville/mesh/service/RadioInterfaceService.kt @@ -197,7 +197,7 @@ class RadioInterfaceService : Service(), Logging { // al proto = MeshProtos.ToRadio.parseFrom(p) debug("sending to radio") - doWrite(BTM_TORADIO_CHARACTER, p) + doAsyncWrite(BTM_TORADIO_CHARACTER, p) if (logSends) { sentPacketsLog.write(p) sentPacketsLog.flush() @@ -337,6 +337,27 @@ class RadioInterfaceService : Service(), Logging { } } + /** + * do an asynchronous write operation + * Any error responses will be ignored (other than log messages) + */ + private fun doAsyncWrite(uuid: UUID, a: ByteArray) = toRemoteExceptions { + if (!isConnected) + throw RadioNotConnectedException() + else { + debug("queuing ${a.size} bytes to $uuid") + + // Note: we generate a new characteristic each time, because we are about to + // change the data and we want the data stored in the closure + val toRadio = service.getCharacteristic(uuid) + toRadio.value = a + + safe!!.asyncWriteCharacteristic(toRadio) { + debug("asyncwrite of ${a.size} bytes completed") + } + } + } + /** * do a synchronous read operation */