Skip to content

Commit 5da13db

Browse files
authored
Fix characteristic observations on Apple and JavaScript targets (#155)
1 parent 00047d9 commit 5da13db

6 files changed

Lines changed: 15 additions & 8 deletions

File tree

core/src/androidMain/kotlin/Peripheral.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public class AndroidPeripheral internal constructor(
205205
discoverServices()
206206
onServicesDiscovered(ServicesDiscoveredPeripheral(this@AndroidPeripheral))
207207
_state.value = State.Connecting.Observes
208-
logger.verbose { message = "rewire" }
208+
logger.verbose { message = "Configuring characteristic observations" }
209209
observers.rewire()
210210
} catch (t: Throwable) {
211211
closeConnection()

core/src/appleMain/kotlin/Observers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal class Observers(
6363
): Flow<NSData> {
6464
return characteristicChanges
6565
.onSubscription {
66-
peripheral.suspendUntil<State.Connecting.Observes>()
66+
peripheral.suspendUntilAtLeast<State.Connecting.Observes>()
6767
if (observations.add(characteristic, onSubscription) == 1) {
6868
peripheral.startNotifications(characteristic)
6969
}

core/src/appleMain/kotlin/Peripheral.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ public class ApplePeripheral internal constructor(
106106
centralManager.delegate
107107
.connectionState
108108
.filter { event -> event.identifier == cbPeripheral.identifier }
109+
.onEach { event ->
110+
logger.debug {
111+
message = "CentralManagerDelegate state change"
112+
detail("state", event.toString())
113+
}
114+
}
109115
.map { event -> event.toState() }
110116
.onEach { _state.value = it }
111117
.launchIn(scope)
@@ -169,8 +175,9 @@ public class ApplePeripheral internal constructor(
169175
suspendUntil<State.Connecting.Services>()
170176
discoverServices()
171177
onServicesDiscovered(ServicesDiscoveredPeripheral(this@ApplePeripheral))
178+
172179
_state.value = State.Connecting.Observes
173-
logger.verbose { message = "rewire" }
180+
logger.verbose { message = "Configuring characteristic observations" }
174181
observers.rewire()
175182
} catch (t: Throwable) {
176183
logger.error(t) { message = "Failed to connect" }
@@ -337,7 +344,7 @@ public class ApplePeripheral internal constructor(
337344

338345
internal suspend fun startNotifications(characteristic: Characteristic) {
339346
logger.debug {
340-
message = "notify"
347+
message = "CentralManager.notify"
341348
detail(characteristic)
342349
}
343350

@@ -349,7 +356,7 @@ public class ApplePeripheral internal constructor(
349356

350357
internal suspend fun stopNotifications(characteristic: Characteristic) {
351358
logger.debug {
352-
message = "cancelNotify"
359+
message = "CentralManager.cancelNotify"
353360
detail(characteristic)
354361
}
355362

core/src/commonMain/kotlin/State.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public sealed class State {
44

55
public sealed class Connecting : State() {
66
/**
7-
* [Peripheral] has initiating the process of connecting, via Bluetooth.
7+
* [Peripheral] has initiated the process of connecting, via Bluetooth.
88
*
99
* I/O operations (e.g. [write][Peripheral.write] and [read][Peripheral.read]) will throw [NotReadyException]
1010
* while in this state.

core/src/jsMain/kotlin/Observers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class Observers(
3636
onSubscription: OnSubscriptionAction,
3737
): Flow<DataView> = characteristicChanges
3838
.onSubscription {
39-
peripheral.suspendUntil<State.Connecting.Observes>()
39+
peripheral.suspendUntilAtLeast<State.Connecting.Observes>()
4040
if (observations.add(characteristic, onSubscription) == 1) {
4141
peripheral.startObservation(characteristic)
4242
}

core/src/jsMain/kotlin/Peripheral.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public class JsPeripheral internal constructor(
134134
discoverServices()
135135
onServicesDiscovered(ServicesDiscoveredPeripheral(this@JsPeripheral))
136136
_state.value = State.Connecting.Observes
137-
logger.verbose { message = "rewire" }
137+
logger.verbose { message = "Configuring characteristic observations" }
138138
observers.rewire()
139139
} catch (t: Throwable) {
140140
logger.error(t) { message = "Failed to connect" }

0 commit comments

Comments
 (0)