Skip to content

Commit

Permalink
[Feature] Add support for registerForConnectionEvents (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
corujautx authored Jul 27, 2024
1 parent cfff039 commit 8161af7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
35 changes: 33 additions & 2 deletions Sources/CentralManager/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ public class CentralManager {
self.cbCentralManager.connect(peripheral.cbPeripheral, options: options)
}
}


@available(macOS, unavailable)
public func registerForConnectionEvents(options: [CBConnectionEventMatchingOption: Any]? = nil) {
self.cbCentralManager.registerForConnectionEvents(options: options)
}

/// Cancels an active or pending local connection to a peripheral.
public func cancelPeripheralConnection(_ peripheral: Peripheral) async throws {
let peripheralState = peripheral.cbPeripheral.state
Expand Down Expand Up @@ -290,7 +295,33 @@ extension CentralManager.DelegateWrapper: CBCentralManagerDelegate {
)
}
}


#if !os(macOS)
func centralManager(
_ cbCentralManager: CBCentralManager,
connectionEventDidOccur event: CBConnectionEvent,
for cbPeripheral: CBPeripheral
) {
let peripheral = Peripheral(cbPeripheral)

switch event {
case .peerConnected:
Self.logger.info("Connection event for \(peripheral.identifier)")
case .peerDisconnected:
Self.logger.info("Disconnection event for \(peripheral.identifier)")
@unknown default:
break
}

self.context.eventSubject.send(
.connectionEventDidOccur(
connectionEvent: event,
peripheral: peripheral
)
)
}
#endif

func centralManager(
_ cbCentralManager: CBCentralManager,
didFailToConnect peripheral: CBPeripheral,
Expand Down
3 changes: 3 additions & 0 deletions Sources/CentralManager/CentralManagerEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ public enum CentralManagerEvent {
case willRestoreState(state: [String: Any])
case didConnectPeripheral(peripheral: Peripheral)
case didDisconnectPeripheral(peripheral: Peripheral, isReconnecting: Bool = false, error: Error?)

@available(macOS, unavailable)
case connectionEventDidOccur(connectionEvent: CBConnectionEvent, peripheral: Peripheral)
}

0 comments on commit 8161af7

Please sign in to comment.