Skip to content

Commit 10a56c0

Browse files
committed
Addressed PR feedback
1 parent b6371b8 commit 10a56c0

4 files changed

Lines changed: 23 additions & 26 deletions

File tree

Demo/ReliaBLE Demo/ReliaBLE Demo/Central/CentralView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ struct CentralView: View {
138138

139139
private var deviceList: some View {
140140
List {
141-
ForEach(Array(devices.enumerated()), id: \.element.persistentModelID) { index, device in
141+
ForEach(devices, id: \.persistentModelID) { device in
142142
NavigationLink {
143143
Group {
144144
let currentState = viewModel.connectionStates[device.id]

Sources/ReliaBLE/BluetoothActor.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ actor BluetoothActor {
668668
log?.warn(tags: [.category(.connection)], "didDisconnect for unknown peripheral — dropped")
669669
return
670670
}
671-
let mappedError: PeripheralError? = payload.error.flatMap { ($0 as? CBError).map { PeripheralError.fromCBError($0) } }
671+
let mappedError: PeripheralError? = payload.error.map { ($0 as? CBError).map(PeripheralError.fromCBError) ?? .unknown }
672672
let state: ConnectionState = .disconnected(reason: mappedError)
673673
connectionStates[id] = state
674674
if let error = mappedError {
@@ -684,7 +684,7 @@ actor BluetoothActor {
684684
log?.warn(tags: [.category(.connection)], "didFailToConnect for unknown peripheral — dropped")
685685
return
686686
}
687-
let mappedError: PeripheralError? = payload.error.flatMap { ($0 as? CBError).map { PeripheralError.fromCBError($0) } }
687+
let mappedError: PeripheralError? = payload.error.map { ($0 as? CBError).map(PeripheralError.fromCBError) ?? .unknown }
688688
let state: ConnectionState = .failed(reason: mappedError)
689689
connectionStates[id] = state
690690
log?.warn(tags: [.peripheral(id), .category(.connection)], "Peripheral connection failed with error: \(mappedError ?? .unknown)")
@@ -780,14 +780,14 @@ final class BluetoothDelegateShim: NSObject, CBCentralManagerDelegate {
780780
let payload = ConnectionPayload(peripheral: peripheral, error: nil)
781781
eventContinuation.yield(.connected(payload))
782782
}
783-
784-
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, timestamp: CFAbsoluteTime, isReconnecting: Bool, error: Error?) {
783+
784+
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
785785
let payload = ConnectionPayload(peripheral: peripheral, error: error)
786-
eventContinuation.yield(.disconnected(payload))
786+
eventContinuation.yield(.connectFailed(payload))
787787
}
788788

789-
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
789+
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, timestamp: CFAbsoluteTime, isReconnecting: Bool, error: Error?) {
790790
let payload = ConnectionPayload(peripheral: peripheral, error: error)
791-
eventContinuation.yield(.connectFailed(payload))
791+
eventContinuation.yield(.disconnected(payload))
792792
}
793793
}

Sources/ReliaBLE/Documentation.docc/GettingStarted.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,24 @@ Use ``ReliaBLEManager/connect(to:)`` to initiate a connection to a discovered ``
147147

148148
```swift
149149
do {
150-
try await bleManager.connect(to: peripheral)
151-
152-
for await change in bleManager.connectionStateChanges
153-
where change.peripheralId == peripheral.id {
154-
switch change.state {
155-
case .connected:
156-
print("Connected to \(peripheral.id)")
157-
case .disconnected(let reason):
158-
print("Disconnected", reason ?? "clean")
159-
case .failed(let reason):
160-
print("Failed", reason ?? "")
161-
default:
162-
break
150+
Task {
151+
for await change in bleManager.connectionStateChanges
152+
where change.peripheralId == peripheral.id {
153+
switch change.state {
154+
case .connected:
155+
print("Connected to \(peripheral.id)")
156+
try await bleManager.disconnect(from: peripheral)
157+
case .disconnected(let reason):
158+
print("Disconnected", reason ?? "clean")
159+
case .failed(let reason):
160+
print("Failed", reason ?? "")
161+
default:
162+
break
163+
}
163164
}
164165
}
165166

166-
try await bleManager.disconnect(from: peripheral)
167+
try await bleManager.connect(to: peripheral)
167168
} catch PeripheralError.notFound {
168169
// The snapshot is stale — its underlying peripheral reference was invalidated
169170
// (for example, after a Bluetooth reset). Re-scan to rediscover it.

Tests/ReliaBLETests/ReliaBLEManagerTests.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,6 @@ final class CapturingLogWriter: LogWriter, @unchecked Sendable {
772772
/// Helpers for driving the Nordic `CBMCentralManagerMock` simulation under the constraints of the
773773
/// process-wide ``BluetoothActor`` singleton.
774774
enum Mock {
775-
/// The resolved ``Peripheral/id`` of the simulated test peripheral.
776-
///
777-
/// The actor resolves a peripheral's id as `name ?? advertisement.localName ?? identifier`. Our spec advertises
778-
/// this exact local name, so the discovered snapshot's `id` is deterministic.
779775
/// The resolved ``Peripheral/id`` of the simulated test peripheral.
780776
///
781777
/// The actor resolves a peripheral's id as `name ?? advertisement.localName ?? identifier`. Our spec advertises

0 commit comments

Comments
 (0)