Skip to content

Commit 8161af7

Browse files
authored
[Feature] Add support for registerForConnectionEvents (#53)
1 parent cfff039 commit 8161af7

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Sources/CentralManager/CentralManager.swift

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ public class CentralManager {
140140
self.cbCentralManager.connect(peripheral.cbPeripheral, options: options)
141141
}
142142
}
143-
143+
144+
@available(macOS, unavailable)
145+
public func registerForConnectionEvents(options: [CBConnectionEventMatchingOption: Any]? = nil) {
146+
self.cbCentralManager.registerForConnectionEvents(options: options)
147+
}
148+
144149
/// Cancels an active or pending local connection to a peripheral.
145150
public func cancelPeripheralConnection(_ peripheral: Peripheral) async throws {
146151
let peripheralState = peripheral.cbPeripheral.state
@@ -290,7 +295,33 @@ extension CentralManager.DelegateWrapper: CBCentralManagerDelegate {
290295
)
291296
}
292297
}
293-
298+
299+
#if !os(macOS)
300+
func centralManager(
301+
_ cbCentralManager: CBCentralManager,
302+
connectionEventDidOccur event: CBConnectionEvent,
303+
for cbPeripheral: CBPeripheral
304+
) {
305+
let peripheral = Peripheral(cbPeripheral)
306+
307+
switch event {
308+
case .peerConnected:
309+
Self.logger.info("Connection event for \(peripheral.identifier)")
310+
case .peerDisconnected:
311+
Self.logger.info("Disconnection event for \(peripheral.identifier)")
312+
@unknown default:
313+
break
314+
}
315+
316+
self.context.eventSubject.send(
317+
.connectionEventDidOccur(
318+
connectionEvent: event,
319+
peripheral: peripheral
320+
)
321+
)
322+
}
323+
#endif
324+
294325
func centralManager(
295326
_ cbCentralManager: CBCentralManager,
296327
didFailToConnect peripheral: CBPeripheral,

Sources/CentralManager/CentralManagerEvent.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ public enum CentralManagerEvent {
88
case willRestoreState(state: [String: Any])
99
case didConnectPeripheral(peripheral: Peripheral)
1010
case didDisconnectPeripheral(peripheral: Peripheral, isReconnecting: Bool = false, error: Error?)
11+
12+
@available(macOS, unavailable)
13+
case connectionEventDidOccur(connectionEvent: CBConnectionEvent, peripheral: Peripheral)
1114
}

0 commit comments

Comments
 (0)