Skip to content

Commit

Permalink
Fixed DarwinCentral.disconnect()
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Oct 10, 2022
1 parent 913b0c9 commit 1c73155
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Sources/DarwinGATT/DarwinCentral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ public final class DarwinCentral: CentralManager, ObservableObject {
let connected = await self.peripherals
.filter { $0.value }
.keys

for peripheral in connected {
await disconnect(peripheral)
}
Expand Down Expand Up @@ -334,14 +333,16 @@ public final class DarwinCentral: CentralManager, ObservableObject {
#if DEBUG
log?("Queue \(function) for peripheral \(peripheral)")
#endif
return try await withThrowingContinuation(for: peripheral, function: function) { continuation in
let value = try await withThrowingContinuation(for: peripheral, function: function) { continuation in
let queuedOperation = QueuedOperation(operation: operation(continuation))
self.async { [unowned self] in
self.stopScan() // stop scanning
let context = self.continuation(for: peripheral)
context.operations.push(queuedOperation)
}
}
try Task.checkCancellation()
return value
}

private func dequeue<Operation>(
Expand All @@ -351,7 +352,7 @@ public final class DarwinCentral: CentralManager, ObservableObject {
filter: (DarwinCentral.Operation) -> (Operation?)
) where Operation: DarwinCentralOperation {
#if DEBUG
log?("Dequeue \(function) for peripheral \(peripheral) with \(result)")
log?("Dequeue \(function) for peripheral \(peripheral)")
#endif
let context = self.continuation(for: peripheral)
context.operations.popFirst(where: { filter($0.operation) }) { (queuedOperation, operation) in
Expand Down Expand Up @@ -803,7 +804,11 @@ internal extension DarwinCentral {
guard let peripheralObject = validatePeripheral(operation.peripheral, for: operation.continuation) else {
return false
}
// connect
// disconnect
guard peripheralObject.state != .disconnected else {
operation.continuation.resume() // already disconnected
return false
}
self.centralManager.cancelPeripheralConnection(peripheralObject)
return true
}
Expand Down Expand Up @@ -1182,7 +1187,6 @@ internal extension DarwinCentral {
let state = unsafeBitCast(centralManager.state, to: DarwinBluetoothState.self)
log("Did update state \(state)")
self.central.objectWillChange.send()
//self.central.continuation.state?.yield(state)
}

@objc(centralManager:didDiscoverPeripheral:advertisementData:RSSI:)
Expand Down

0 comments on commit 1c73155

Please sign in to comment.