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

Throw exception when Android's BluetoothAdapter is not On #137

Merged
merged 3 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions core/api/core.api
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public final class com/juul/kable/AndroidScanner : com/juul/kable/Scanner {
public fun getAdvertisements ()Lkotlinx/coroutines/flow/Flow;
}

public class com/juul/kable/BluetoothLeException : java/lang/Exception {
public final class com/juul/kable/BluetoothDisabledException : com/juul/kable/BluetoothException {
public fun <init> ()V
}

public class com/juul/kable/BluetoothException : java/lang/Exception {
public fun <init> ()V
}

Expand Down Expand Up @@ -97,7 +101,7 @@ public final class com/juul/kable/DiscoveredService : com/juul/kable/Service {
public fun toString ()Ljava/lang/String;
}

public final class com/juul/kable/GattRequestRejectedException : com/juul/kable/BluetoothLeException {
public final class com/juul/kable/GattRequestRejectedException : com/juul/kable/BluetoothException {
public fun <init> ()V
}

Expand Down
4 changes: 3 additions & 1 deletion core/src/androidMain/kotlin/Exceptions.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:JvmName("AndroidExceptionsKt") // Removes conflict with commonMain Exceptions.kt

package com.juul.kable

import android.bluetooth.BluetoothDevice
Expand All @@ -18,4 +20,4 @@ public actual typealias IOException = java.io.IOException
public class GattRequestRejectedException internal constructor(
message: String? = null,
cause: Throwable? = null,
) : BluetoothLeException(message, cause)
) : BluetoothException(message, cause)
28 changes: 28 additions & 0 deletions core/src/androidMain/kotlin/Peripheral.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.juul.kable

import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothAdapter.STATE_OFF
import android.bluetooth.BluetoothAdapter.STATE_ON
import android.bluetooth.BluetoothAdapter.STATE_TURNING_OFF
import android.bluetooth.BluetoothAdapter.STATE_TURNING_ON
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGatt
import android.bluetooth.BluetoothGattCharacteristic.PROPERTY_INDICATE
Expand Down Expand Up @@ -197,6 +201,7 @@ public class AndroidPeripheral internal constructor(

public override suspend fun connect() {
check(job.isNotCancelled) { "Cannot connect, scope is cancelled for $this" }
checkBluetoothAdapterState(expected = STATE_ON)
connectJob.updateAndGet { it ?: connectAsync() }!!.await()
}

Expand Down Expand Up @@ -377,3 +382,26 @@ private val PlatformCharacteristic.supportsNotify: Boolean

private val PlatformCharacteristic.supportsIndicate: Boolean
get() = bluetoothGattCharacteristic.properties and PROPERTY_INDICATE != 0

/**
* 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.")
}
}
12 changes: 10 additions & 2 deletions core/src/commonMain/kotlin/Exceptions.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.juul.kable

/** Failure occurred with the underlying Bluetooth Low Energy system. */
public open class BluetoothLeException internal constructor(
/** Failure occurred with the underlying Bluetooth system. */
public open class BluetoothException internal constructor(
message: String? = null,
cause: Throwable? = null,
) : Exception(message, cause)

@Deprecated("Class has been renamed.", ReplaceWith("BluetoothException", "com.juul.kable.BluetoothException"))
public typealias BluetoothLeException = BluetoothException

public class BluetoothDisabledException internal constructor(
message: String? = null,
cause: Throwable? = null,
) : BluetoothException(message, cause)

public expect open class IOException internal constructor(
message: String? = null,
cause: Throwable? = null,
Expand Down