From cc05be7d87256255ccc184e6fffbee37eab5629b Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 4 Jul 2020 11:02:18 -0700 Subject: [PATCH] bug #76 fix a related autobug from feh123's log Non-fatal Exception: kotlin.KotlinNullPointerException at com.geeksville.mesh.service.BluetoothInterface.doDiscoverServicesAndInit(BluetoothInterface.java:357) at com.geeksville.mesh.service.BluetoothInterface.access$doDiscoverServicesAndInit(BluetoothInterface.java:78) at com.geeksville.mesh.service.BluetoothInterface$onConnect$1.invokeSuspend(BluetoothInterface.java:429) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:56) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:571) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.java:738) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.java:678) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.java:665) --- .../mesh/service/BluetoothInterface.kt | 58 ++++++++++--------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt b/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt index 5a3cac3b7..235a6e58c 100644 --- a/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt +++ b/app/src/main/java/com/geeksville/mesh/service/BluetoothInterface.kt @@ -354,42 +354,46 @@ class BluetoothInterface(val service: RadioInterfaceService, val address: String private var isFirstTime = true private fun doDiscoverServicesAndInit() { - safe!!.asyncDiscoverServices { discRes -> - try { - discRes.getOrThrow() + val s = safe + if (s == null) + warn("Interface is shutting down, so skipping discover") + else + s.asyncDiscoverServices { discRes -> + try { + discRes.getOrThrow() - service.serviceScope.handledLaunch { - try { - debug("Discovered services!") - delay(1000) // android BLE is buggy and needs a 500ms sleep before calling getChracteristic, or you might get back null + service.serviceScope.handledLaunch { + try { + debug("Discovered services!") + delay(1000) // android BLE is buggy and needs a 500ms sleep before calling getChracteristic, or you might get back null - /* if (isFirstTime) { - isFirstTime = false - throw BLEException("Faking a BLE failure") - } */ + /* if (isFirstTime) { + isFirstTime = false + throw BLEException("Faking a BLE failure") + } */ - fromNum = getCharacteristic(BTM_FROMNUM_CHARACTER) + fromNum = getCharacteristic(BTM_FROMNUM_CHARACTER) - // We treat the first send by a client as special - isFirstSend = true + // We treat the first send by a client as special + isFirstSend = true - // Now tell clients they can (finally use the api) - service.onConnect() + // Now tell clients they can (finally use the api) + service.onConnect() - // Immediately broadcast any queued packets sitting on the device - doReadFromRadio(true) - } catch (ex: BLEException) { - scheduleReconnect( - "Unexpected error in initial device enumeration, forcing disconnect $ex" - ) + // Immediately broadcast any queued packets sitting on the device + doReadFromRadio(true) + } catch (ex: BLEException) { + scheduleReconnect( + "Unexpected error in initial device enumeration, forcing disconnect $ex" + ) + } } + } catch (ex: BLEException) { + scheduleReconnect( + "Unexpected error discovering services, forcing disconnect $ex" + ) } - } catch (ex: BLEException) { - scheduleReconnect( - "Unexpected error discovering services, forcing disconnect $ex" - ) } - } } private var needForceRefresh = true