Skip to content

Commit 7ffd7cb

Browse files
authored
Drop ConnectionRejectedException (#771)
1 parent f6092de commit 7ffd7cb

8 files changed

Lines changed: 27 additions & 27 deletions

File tree

kable-core/api/android/kable-core.api

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,6 @@ public final class com/juul/kable/Characteristic$Properties {
108108
public final synthetic fun unbox-impl ()I
109109
}
110110

111-
public final class com/juul/kable/ConnectionRejectedException : java/io/IOException {
112-
public fun <init> ()V
113-
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
114-
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
115-
}
116-
117111
public abstract interface class com/juul/kable/Descriptor {
118112
public abstract fun getCharacteristicUuid ()Ljava/util/UUID;
119113
public abstract fun getDescriptorUuid ()Ljava/util/UUID;

kable-core/api/jvm/kable-core.api

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ public final class com/juul/kable/Characteristic$Properties {
6363
public final synthetic fun unbox-impl ()I
6464
}
6565

66-
public final class com/juul/kable/ConnectionRejectedException : java/io/IOException {
67-
public fun <init> ()V
68-
public fun <init> (Ljava/lang/String;Ljava/lang/Throwable;)V
69-
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
70-
}
71-
7266
public abstract interface class com/juul/kable/Descriptor {
7367
public abstract fun getCharacteristicUuid ()Ljava/util/UUID;
7468
public abstract fun getDescriptorUuid ()Ljava/util/UUID;

kable-core/src/androidMain/kotlin/BluetoothDevice.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.juul.kable.gatt.Callback
1616
import com.juul.kable.logs.Logging
1717
import kotlinx.coroutines.flow.MutableSharedFlow
1818
import kotlinx.coroutines.flow.MutableStateFlow
19+
import kotlinx.io.IOException
1920
import kotlin.coroutines.CoroutineContext
2021
import kotlin.time.Duration
2122

@@ -36,7 +37,7 @@ internal fun BluetoothDevice.connect(
3637
logging: Logging,
3738
threadingStrategy: ThreadingStrategy,
3839
disconnectTimeout: Duration,
39-
): Connection? {
40+
): Connection {
4041
val callback = Callback(state, mtu, onCharacteristicChanged, logging, address)
4142
val threading = threadingStrategy.acquire()
4243

@@ -52,17 +53,12 @@ internal fun BluetoothDevice.connect(
5253
?: connectGattCompat(context, true, callback, transport.intValue)
5354

5455
else -> connectGattCompat(context, autoConnect, callback, transport.intValue)
55-
}
56+
} ?: throw IOException("Binder remote-invocation error")
5657
} catch (t: Throwable) {
5758
threading.release()
5859
throw t
5960
}
6061

61-
if (bluetoothGatt == null) {
62-
threading.release()
63-
return null
64-
}
65-
6662
return Connection(coroutineContext, bluetoothGatt, threading, callback, services, disconnectTimeout, logging)
6763
}
6864

kable-core/src/androidMain/kotlin/BluetoothDeviceAndroidPeripheral.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ import com.juul.kable.State.Disconnected
2121
import com.juul.kable.WriteType.WithResponse
2222
import com.juul.kable.WriteType.WithoutResponse
2323
import com.juul.kable.bluetooth.checkBluetoothIsOn
24+
import com.juul.kable.bluetooth.checkBluetoothIsSupported
2425
import com.juul.kable.bluetooth.clientCharacteristicConfigUuid
26+
import com.juul.kable.bluetooth.requireNonZeroAddress
2527
import com.juul.kable.gatt.Response.OnCharacteristicRead
2628
import com.juul.kable.gatt.Response.OnCharacteristicWrite
2729
import com.juul.kable.gatt.Response.OnDescriptorRead
@@ -92,13 +94,14 @@ internal class BluetoothDeviceAndroidPeripheral(
9294
override val type: Type
9395
get() = typeFrom(bluetoothDevice.type)
9496

95-
override val address: String = bluetoothDevice.address
97+
override val address: String = requireNonZeroAddress(bluetoothDevice.address)
9698

9799
@ExperimentalApi
98100
override val name: String?
99101
get() = bluetoothDevice.name
100102

101103
private suspend fun establishConnection(scope: CoroutineScope): CoroutineScope {
104+
checkBluetoothIsSupported()
102105
checkBluetoothIsOn()
103106

104107
logger.info { message = "Connecting" }
@@ -118,7 +121,7 @@ internal class BluetoothDeviceAndroidPeripheral(
118121
logging,
119122
threadingStrategy,
120123
disconnectTimeout,
121-
) ?: throw ConnectionRejectedException()
124+
)
122125

123126
suspendUntil<State.Connecting.Services>()
124127
discoverServices()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.juul.kable.bluetooth
2+
3+
import android.bluetooth.BluetoothDevice
4+
5+
private const val ZERO_MAC_ADDRESS = "00:00:00:00:00:00"
6+
7+
/** Performs the same MAC address validation as performed in [BluetoothDevice.connectGatt]. */
8+
internal fun requireNonZeroAddress(address: String): String {
9+
require(ZERO_MAC_ADDRESS != address) { "Invalid address: $address" }
10+
return address
11+
}

kable-core/src/commonMain/kotlin/Exceptions.kt renamed to kable-core/src/commonMain/kotlin/GattStatusException.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ package com.juul.kable
22

33
import kotlinx.io.IOException
44

5-
public class ConnectionRejectedException(
6-
message: String? = null,
7-
cause: Throwable? = null,
8-
) : IOException(message, cause)
9-
105
public class GattStatusException(
116
message: String? = null,
127
cause: Throwable? = null,

kable-core/src/commonMain/kotlin/Peripheral.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,9 @@ public interface Peripheral : CoroutineScope {
119119
* supervisor scope, meaning any failures in launched coroutines will not fail other launched
120120
* coroutines nor cause a disconnect.
121121
*
122-
* @throws ConnectionRejectedException when a connection request is rejected by the system (e.g. bluetooth hardware unavailable).
122+
* @throws IllegalStateException when a connection request could not be made (e.g. bluetooth not supported).
123+
* @throws NotConnectedException if unable to establish connection (e.g. connection lost while discovering services).
124+
* @throws IOException (Android) if request failed due to Binder remote-invocation error.
123125
* @throws CancellationException if [Peripheral]'s [CoroutineScope] has been [cancelled][Peripheral.cancel].
124126
*/
125127
public suspend fun connect(): CoroutineScope
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.juul.kable.bluetooth
2+
3+
internal suspend fun checkBluetoothIsSupported() {
4+
check(isSupported()) { "Bluetooth not supported" }
5+
}

0 commit comments

Comments
 (0)