NordicSemiconductor / Kotlin-BLE-Library

BSD 3-Clause "New" or "Revised" License
310 stars 33 forks source link

java.lang.IllegalStateException: BT Adapter is not turned ON - When bluetooth is turned on when it was previously off #158

Open duranaustin opened 4 weeks ago

duranaustin commented 4 weeks ago

This is not a consistent error. It happens here and there. It feels like maybe a race condition? If bluetooth is turned off and it is turned back on, and we trigger a scan immediately, this error happens.

java.lang.IllegalStateException: BT Adapter is not turned ON
   at android.bluetooth.le.BluetoothLeUtils.checkAdapterStateOn(BluetoothLeUtils.java:150)
   at android.bluetooth.le.BluetoothLeScanner.stopScan(BluetoothLeScanner.java:306)
   at no.nordicsemi.android.kotlin.ble.scanner.BleScanner$scan$1.invokeSuspend$lambda$2(BleScanner.kt:127)
   at no.nordicsemi.android.kotlin.ble.scanner.BleScanner$scan$1.$r8$lambda$eeoutcJcmSizYqUES9njj-yPplo(Unknown Source:0)
   at no.nordicsemi.android.kotlin.ble.scanner.BleScanner$scan$1$$ExternalSyntheticLambda0.invoke(Unknown Source:4)
   at kotlinx.coroutines.channels.ProduceKt.awaitClose(Produce.kt:51)
   at kotlinx.coroutines.channels.ProduceKt$awaitClose$1.invokeSuspend(Unknown Source:13)
   at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
   at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:229)
   at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:185)
   at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:157)
   at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:470)
   at kotlinx.coroutines.CancellableContinuationImpl.cancel(CancellableContinuationImpl.kt:214)
   at kotlinx.coroutines.CancellableContinuationImpl.parentCancelled$kotlinx_coroutines_core(CancellableContinuationImpl.kt:221)
   at kotlinx.coroutines.ChildContinuation.invoke(CancellableContinuationImpl.kt:696)
   at kotlinx.coroutines.JobSupport.notifyCancelling(JobSupport.kt:1603)
   at kotlinx.coroutines.JobSupport.tryMakeCancelling(JobSupport.kt:826)
   at kotlinx.coroutines.JobSupport.makeCancelling(JobSupport.kt:786)
   at kotlinx.coroutines.JobSupport.cancelImpl$kotlinx_coroutines_core(JobSupport.kt:702)
   at kotlinx.coroutines.JobSupport.parentCancelled(JobSupport.kt:668)
   at kotlinx.coroutines.ChildHandleNode.invoke(JobSupport.kt:1578)
   at kotlinx.coroutines.JobSupport.notifyCancelling(JobSupport.kt:1603)
   at kotlinx.coroutines.JobSupport.tryMakeCancelling(JobSupport.kt:826)
   at kotlinx.coroutines.JobSupport.makeCancelling(JobSupport.kt:786)
   at kotlinx.coroutines.JobSupport.cancelImpl$kotlinx_coroutines_core(JobSupport.kt:702)
   at kotlinx.coroutines.JobSupport.parentCancelled(JobSupport.kt:668)
   at kotlinx.coroutines.ChildHandleNode.invoke(JobSupport.kt:1578)
   at kotlinx.coroutines.JobSupport.notifyCancelling(JobSupport.kt:1603)
   at kotlinx.coroutines.JobSupport.tryMakeCancelling(JobSupport.kt:826)
   at kotlinx.coroutines.JobSupport.makeCancelling(JobSupport.kt:786)
   at kotlinx.coroutines.JobSupport.cancelImpl$kotlinx_coroutines_core(JobSupport.kt:702)
   at kotlinx.coroutines.JobSupport.parentCancelled(JobSupport.kt:668)
   at kotlinx.coroutines.ChildHandleNode.invoke(JobSupport.kt:1578)
   at kotlinx.coroutines.JobSupport.notifyCancelling(JobSupport.kt:1603)
   at kotlinx.coroutines.JobSupport.tryMakeCancelling(JobSupport.kt:826)
   at kotlinx.coroutines.JobSupport.makeCancelling(JobSupport.kt:786)
   at kotlinx.coroutines.JobSupport.cancelImpl$kotlinx_coroutines_core(JobSupport.kt:702)
   at kotlinx.coroutines.JobSupport.cancelInternal(JobSupport.kt:663)
   at kotlinx.coroutines.JobSupport.cancel(JobSupport.kt:648)
   at kotlinx.coroutines.Job$DefaultImpls.cancel$default(Job.kt:192)
   at com.gabb.mygabbandroid.presentation.viewmodel.bluetooth.BluetoothViewModel.stopScan(BluetoothViewModel.kt:171)
   at com.gabb.mygabbandroid.presentation.ui.navigation.navgraph.AddWatchThreeGraphKt$addWatchThreeGraph$1$1$15.invoke(AddWatchThreeGraph.kt:117)
   at com.gabb.mygabbandroid.presentation.ui.navigation.navgraph.AddWatchThreeGraphKt$addWatchThreeGraph$1$1$15.invoke(AddWatchThreeGraph.kt:58)
   at com.gabb.mygabbandroid.presentation.ui.addwatchthree.BluetoothScreenKt$BluetoothScreen$3.invokeSuspend(BluetoothScreen.kt:93)
   at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
   at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:102)
2024-08-15 10:44:01.420 21545-21545 AndroidRuntime          com.gabb.my_gabb                     E      at androidx.compose.ui.platform.AndroidUiDispatcher.performTrampolineDispatch(AndroidUiDispatcher.android.kt:81) (Ask Gemini)
   at androidx.compose.ui.platform.AndroidUiDispatcher.access$performTrampolineDispatch(AndroidUiDispatcher.android.kt:41)
   at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.run(AndroidUiDispatcher.android.kt:57)
   at android.os.Handler.handleCallback(Handler.java:942)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loopOnce(Looper.java:201)
   at android.os.Looper.loop(Looper.java:288)
   at android.app.ActivityThread.main(ActivityThread.java:7872)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
   Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@fae36b3, Dispatchers.Main.immediate]
philips77 commented 4 weeks ago

Hello, Please try the new version of the library, available on branch version/2. We're working on it actively and it will soon replace the current one. I hope the API will be more readable and simpler.