diff --git a/app/src/main/java/com/geeksville/mesh/repository/radio/BluetoothInterface.kt b/app/src/main/java/com/geeksville/mesh/repository/radio/BluetoothInterface.kt index 31a85d851..d9b66dd95 100644 --- a/app/src/main/java/com/geeksville/mesh/repository/radio/BluetoothInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/repository/radio/BluetoothInterface.kt @@ -94,6 +94,8 @@ class BluetoothInterface @AssistedInject constructor( UUID.fromString("f75c76d2-129e-4dad-a1dd-7866124401e7") val BTM_FROMNUM_CHARACTER: UUID = UUID.fromString("ed9da18c-a800-4f66-a670-aa7547e34453") + val BTM_LOGRADIO_CHARACTER: UUID = + UUID.fromString("6c6fd238-78fa-436b-aacf-15c5be1ef2e2") /** * this is created in onCreate() @@ -115,6 +117,7 @@ class BluetoothInterface @AssistedInject constructor( ?: throw RadioNotConnectedException("BLE service not found") private lateinit var fromNum: BluetoothGattCharacteristic + private lateinit var logRadio: BluetoothGattCharacteristic /** * With the new rev2 api, our first send is to start the configure readbacks. In that case, @@ -258,6 +261,37 @@ class BluetoothInterface @AssistedInject constructor( } } + private fun startWatchingLogRadio() { + safe?.setNotify(logRadio, true) { + service.serviceScope.handledLaunch { + safe?.asyncReadCharacteristic(logRadio) { + try { + val b = it.getOrThrow() + .value.clone() // We clone the array just in case, I'm not sure if they keep reusing the array + + if (b.isNotEmpty()) { + val logMessage = b.toString(Charsets.UTF_8) + + if (logMessage.startsWith("INFO")) + info(logMessage.replace("INFO | ", "")) + else if (logMessage.startsWith("DEBUG")) + debug(logMessage.replace("DEBUG | ", "")) + else if (logMessage.startsWith("WARN")) + warn(logMessage.replace("WARN | ", "")) + else if (logMessage.startsWith("ERROR")) + error(logMessage.replace("ERROR | ", "")) + else + debug(logMessage) + } + + } catch (ex: BLEException) { + scheduleReconnect("error during doReadFromRadio - disconnecting, ${ex.message}") + } + } + } + } + } + /** * Some buggy BLE stacks can fail on initial connect, with either missing services or missing characteristics. If that happens we * disconnect and try again when the device reenumerates. @@ -321,6 +355,7 @@ class BluetoothInterface @AssistedInject constructor( } */ fromNum = getCharacteristic(BTM_FROMNUM_CHARACTER) + logRadio = getCharacteristic(BTM_LOGRADIO_CHARACTER) // We treat the first send by a client as special isFirstSend = true @@ -330,6 +365,7 @@ class BluetoothInterface @AssistedInject constructor( // Immediately broadcast any queued packets sitting on the device doReadFromRadio(true) + startWatchingLogRadio() } catch (ex: BLEException) { scheduleReconnect( "Unexpected error in initial device enumeration, forcing disconnect $ex"