Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 0334b37

Browse files
committed
Add characteristic change BroadcastChannel
`Flow`'s `onStart` is called **after** subscribing to underlying data stream. When performing a send-then-receive the response could be lost if the `Flow` is slow to subscribe. Problem was described/discussed in [Kotlin/kotlinx.coroutines#1758] and will be fixed in [Kotlin/kotlinx.coroutines#2034]. The workaround is to use `openSubscription().consumeAsFlow()` which opens the subscription prior to spinning up the `Flow` (and in turn will be subscribed before `onStart`). [Kotlin/kotlinx.coroutines#1758]: Kotlin/kotlinx.coroutines#1758 (comment) [Kotlin/kotlinx.coroutines#2034]: Kotlin/kotlinx.coroutines#2034
1 parent 2c407fb commit 0334b37

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

core/src/main/java/gatt/CoroutinesGatt.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.util.UUID
1515
import kotlinx.coroutines.CancellationException
1616
import kotlinx.coroutines.ExecutorCoroutineDispatcher
1717
import kotlinx.coroutines.FlowPreview
18+
import kotlinx.coroutines.channels.BroadcastChannel
1819
import kotlinx.coroutines.flow.asFlow
1920
import kotlinx.coroutines.sync.Mutex
2021
import kotlinx.coroutines.sync.withLock
@@ -36,8 +37,15 @@ class CoroutinesGatt internal constructor(
3637
@FlowPreview
3738
override val onConnectionStateChange = callback.onConnectionStateChange.asFlow()
3839

40+
@Deprecated(
41+
message = "Will be removed when Kotlin/kotlinx.coroutines#2034 is closed; use onCharacteristicChanged instead.",
42+
replaceWith = ReplaceWith(expression = "onCharacteristicChanged")
43+
)
44+
override val onCharacteristicChangedChannel: BroadcastChannel<OnCharacteristicChanged>
45+
get() = callback.onCharacteristicChanged
46+
3947
@FlowPreview
40-
override val onCharacteristicChanged = callback.onCharacteristicChanged.asFlow()
48+
override val onCharacteristicChanged = onCharacteristicChangedChannel.asFlow()
4149

4250
override val services: List<BluetoothGattService> get() = bluetoothGatt.services
4351
override fun getService(uuid: UUID): BluetoothGattService? = bluetoothGatt.getService(uuid)

core/src/main/java/gatt/GattIo.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.bluetooth.BluetoothGattService
1212
import android.os.RemoteException
1313
import java.util.UUID
1414
import kotlinx.coroutines.FlowPreview
15+
import kotlinx.coroutines.channels.BroadcastChannel
1516
import kotlinx.coroutines.flow.Flow
1617

1718
/**
@@ -50,6 +51,13 @@ interface GattIo {
5051
val services: List<BluetoothGattService>
5152
fun getService(uuid: UUID): BluetoothGattService?
5253

54+
/** Will be removed when https://github.com/Kotlin/kotlinx.coroutines/issues/2034 is closed. */
55+
@Deprecated(
56+
message = "Will be removed when Kotlin/kotlinx.coroutines#2034 is closed; use onCharacteristicChanged instead.",
57+
replaceWith = ReplaceWith(expression = "onCharacteristicChanged")
58+
)
59+
val onCharacteristicChangedChannel: BroadcastChannel<OnCharacteristicChanged>
60+
5361
@FlowPreview
5462
val onCharacteristicChanged: Flow<OnCharacteristicChanged>
5563

0 commit comments

Comments
 (0)