Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crash for Android 12 #366

Closed
keerthankrrao opened this issue Aug 1, 2022 · 6 comments
Closed

App crash for Android 12 #366

keerthankrrao opened this issue Aug 1, 2022 · 6 comments

Comments

@keerthankrrao
Copy link

keerthankrrao commented Aug 1, 2022

Experiencing app crash at Android 12, but it works fine for < Android OS v12. I've added the permissions for Android 12 too i.e, Bluetooth Connect and Bluetooth Scan.

2022-07-28 16:25:36.966 15165-15297/ E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
com.juul.kable.BluetoothDisabledException: Bluetooth adapter state is Off (10), but On (12) was required.
at com.juul.kable.PeripheralKt.checkBluetoothAdapterState(Peripheral.kt:499)
at com.juul.kable.PeripheralKt.access$checkBluetoothAdapterState(Peripheral.kt:1)
at com.juul.kable.AndroidPeripheral.connect(Peripheral.kt:231)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:749)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@e20bf2e, Dispatchers.IO]

@twyatt
Copy link
Member

twyatt commented Aug 1, 2022

It appears that the phone's bluetooth is turned off.

This is checked via:

/**
* Explicitly check the adapter state before connecting in order to respect system settings.
* Android doesn't actually turn bluetooth off when the setting is disabled, so without this
* check we're able to reconnect the device illegally.
*/
private fun checkBluetoothAdapterState(
expected: Int,
) {
fun nameFor(value: Int) = when (value) {
STATE_OFF -> "Off"
STATE_ON -> "On"
STATE_TURNING_OFF -> "TurningOff"
STATE_TURNING_ON -> "TurningOn"
else -> "Unknown"
}
val actual = BluetoothAdapter.getDefaultAdapter().state
if (expected != actual) {
val actualName = nameFor(actual)
val expectedName = nameFor(expected)
throw BluetoothDisabledException("Bluetooth adapter state is $actualName ($actual), but $expectedName ($expected) was required.")
}
}

Does this happen on all devices you've tried, or is it specific to certain manufactures or models?
I vaguely remember a specific model would misreport this, and the only workaround was the catch this exception in that situation. 😢

@keerthankrrao
Copy link
Author

Thank you the update. It got worked after adding the permissions for Android 12 and enabling the bluetooth adapter.

@twyatt
Copy link
Member

twyatt commented Jan 5, 2023

#335 might help in the future for checking if bluetooth is ready to use.

@twyatt twyatt closed this as not planned Won't fix, can't repro, duplicate, stale Jan 5, 2023
@degill
Copy link
Contributor

degill commented Dec 7, 2023

Sorry for hijacking this Issue @twyatt, but would it make sense to document BluetoothDisabledException as another possible exception on Peripheral.connect? Currently only ConnectionRejectedException and CancellationException are documented.

To make my intrusion worse: I went through the code and got the following questions, which I can turn into their own issues if thats a better place for them. They both are about documentation.

  1. It looks liks Scanner.advertisements can throw BluetoothDisabledException and ScanFailedException, which I believe would crash my app if the CoroutineContext with which I collect advertisements does not use a SupervisorJob and if I don't use Flow.catch. Would it make sense to document these exceptions on that property?
  2. (Nearly same as my initial question) If I am not mistaken then Peripheral.connect can throw ConnectionRejectedException, BluetoothDisabledException and ConnectionLostException. Should these be documented?

I can create a PR with such documentation if desired.

@twyatt
Copy link
Member

twyatt commented Dec 8, 2023

I'm traveling at the moment, so I'll respond more thoroughly after I get back, but not a problem at all "hijacking" this issue. Love all the help in identifying documentation shortcomings. PRs very welcome.

@twyatt
Copy link
Member

twyatt commented Dec 21, 2023

@degill so this doesn't get lost, created #614 to track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants