Skip to content

Commit 2cc90fe

Browse files
NickKibishgithub-actions[bot]
authored andcommitted
Copied files from native CoreBluetooth version to CoreBluetoothMock
1 parent 6831f7d commit 2cc90fe

File tree

8 files changed

+766
-723
lines changed

8 files changed

+766
-723
lines changed

Sources/iOS-BLE-Library-Mock/Alias.swift

+48-39
Original file line numberDiff line numberDiff line change
@@ -38,50 +38,59 @@ import CoreBluetoothMock
3838
// disabled for Xcode 12.5 beta
3939
//typealias CBPeer = CBMPeer
4040
//typealias CBAttribute = CBMAttribute
41-
public typealias CBCentralManagerFactory = CBMCentralManagerFactory
42-
public typealias CBUUID = CBMUUID
43-
public typealias CBError = CBMError
44-
public typealias CBATTError = CBMATTError
45-
public typealias CBManagerState = CBMManagerState
46-
public typealias CBPeripheralState = CBMPeripheralState
47-
public typealias CBCentralManager = CBMCentralManager
48-
public typealias CBCentralManagerDelegate = CBMCentralManagerDelegate
49-
public typealias CBPeripheral = CBMPeripheral
50-
public typealias CBPeripheralDelegate = CBMPeripheralDelegate
51-
public typealias CBService = CBMService
52-
public typealias CBCharacteristic = CBMCharacteristic
53-
public typealias CBCharacteristicWriteType = CBMCharacteristicWriteType
54-
public typealias CBCharacteristicProperties = CBMCharacteristicProperties
55-
public typealias CBDescriptor = CBMDescriptor
56-
public typealias CBConnectionEvent = CBMConnectionEvent
41+
public typealias CBCentralManagerFactory = CBMCentralManagerFactory
42+
public typealias CBUUID = CBMUUID
43+
public typealias CBError = CBMError
44+
public typealias CBATTError = CBMATTError
45+
public typealias CBManagerState = CBMManagerState
46+
public typealias CBPeripheralState = CBMPeripheralState
47+
public typealias CBCentralManager = CBMCentralManager
48+
public typealias CBCentralManagerDelegate = CBMCentralManagerDelegate
49+
public typealias CBPeripheral = CBMPeripheral
50+
public typealias CBPeripheralDelegate = CBMPeripheralDelegate
51+
public typealias CBService = CBMService
52+
public typealias CBCharacteristic = CBMCharacteristic
53+
public typealias CBCharacteristicWriteType = CBMCharacteristicWriteType
54+
public typealias CBCharacteristicProperties = CBMCharacteristicProperties
55+
public typealias CBDescriptor = CBMDescriptor
56+
public typealias CBConnectionEvent = CBMConnectionEvent
5757
public typealias CBConnectionEventMatchingOption = CBMConnectionEventMatchingOption
5858
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, *)
59-
public typealias CBL2CAPPSM = CBML2CAPPSM
59+
public typealias CBL2CAPPSM = CBML2CAPPSM
6060
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, *)
61-
public typealias CBL2CAPChannel = CBML2CAPChannel
61+
public typealias CBL2CAPChannel = CBML2CAPChannel
6262

63-
public let CBCentralManagerScanOptionAllowDuplicatesKey = CBMCentralManagerScanOptionAllowDuplicatesKey
64-
public let CBCentralManagerOptionShowPowerAlertKey = CBMCentralManagerOptionShowPowerAlertKey
65-
public let CBCentralManagerOptionRestoreIdentifierKey = CBMCentralManagerOptionRestoreIdentifierKey
66-
public let CBCentralManagerScanOptionSolicitedServiceUUIDsKey = CBMCentralManagerScanOptionSolicitedServiceUUIDsKey
67-
public let CBConnectPeripheralOptionStartDelayKey = CBMConnectPeripheralOptionStartDelayKey
63+
public let CBCentralManagerScanOptionAllowDuplicatesKey =
64+
CBMCentralManagerScanOptionAllowDuplicatesKey
65+
public let CBCentralManagerOptionShowPowerAlertKey = CBMCentralManagerOptionShowPowerAlertKey
66+
public let CBCentralManagerOptionRestoreIdentifierKey = CBMCentralManagerOptionRestoreIdentifierKey
67+
public let CBCentralManagerScanOptionSolicitedServiceUUIDsKey =
68+
CBMCentralManagerScanOptionSolicitedServiceUUIDsKey
69+
public let CBConnectPeripheralOptionStartDelayKey = CBMConnectPeripheralOptionStartDelayKey
6870
#if !os(macOS)
69-
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
70-
public let CBConnectPeripheralOptionRequiresANCS = CBMConnectPeripheralOptionRequiresANCS
71+
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
72+
public let CBConnectPeripheralOptionRequiresANCS = CBMConnectPeripheralOptionRequiresANCS
7173
#endif
72-
public let CBCentralManagerRestoredStatePeripheralsKey = CBMCentralManagerRestoredStatePeripheralsKey
73-
public let CBCentralManagerRestoredStateScanServicesKey = CBMCentralManagerRestoredStateScanServicesKey
74-
public let CBCentralManagerRestoredStateScanOptionsKey = CBMCentralManagerRestoredStateScanOptionsKey
74+
public let CBCentralManagerRestoredStatePeripheralsKey =
75+
CBMCentralManagerRestoredStatePeripheralsKey
76+
public let CBCentralManagerRestoredStateScanServicesKey =
77+
CBMCentralManagerRestoredStateScanServicesKey
78+
public let CBCentralManagerRestoredStateScanOptionsKey =
79+
CBMCentralManagerRestoredStateScanOptionsKey
7580

76-
public let CBAdvertisementDataLocalNameKey = CBMAdvertisementDataLocalNameKey
77-
public let CBAdvertisementDataServiceUUIDsKey = CBMAdvertisementDataServiceUUIDsKey
78-
public let CBAdvertisementDataIsConnectable = CBMAdvertisementDataIsConnectable
79-
public let CBAdvertisementDataTxPowerLevelKey = CBMAdvertisementDataTxPowerLevelKey
80-
public let CBAdvertisementDataServiceDataKey = CBMAdvertisementDataServiceDataKey
81-
public let CBAdvertisementDataManufacturerDataKey = CBMAdvertisementDataManufacturerDataKey
82-
public let CBAdvertisementDataOverflowServiceUUIDsKey = CBMAdvertisementDataOverflowServiceUUIDsKey
83-
public let CBAdvertisementDataSolicitedServiceUUIDsKey = CBMAdvertisementDataSolicitedServiceUUIDsKey
81+
public let CBAdvertisementDataLocalNameKey = CBMAdvertisementDataLocalNameKey
82+
public let CBAdvertisementDataServiceUUIDsKey = CBMAdvertisementDataServiceUUIDsKey
83+
public let CBAdvertisementDataIsConnectable = CBMAdvertisementDataIsConnectable
84+
public let CBAdvertisementDataTxPowerLevelKey = CBMAdvertisementDataTxPowerLevelKey
85+
public let CBAdvertisementDataServiceDataKey = CBMAdvertisementDataServiceDataKey
86+
public let CBAdvertisementDataManufacturerDataKey = CBMAdvertisementDataManufacturerDataKey
87+
public let CBAdvertisementDataOverflowServiceUUIDsKey = CBMAdvertisementDataOverflowServiceUUIDsKey
88+
public let CBAdvertisementDataSolicitedServiceUUIDsKey =
89+
CBMAdvertisementDataSolicitedServiceUUIDsKey
8490

85-
public let CBConnectPeripheralOptionNotifyOnConnectionKey = CBMConnectPeripheralOptionNotifyOnConnectionKey
86-
public let CBConnectPeripheralOptionNotifyOnDisconnectionKey = CBMConnectPeripheralOptionNotifyOnDisconnectionKey
87-
public let CBConnectPeripheralOptionNotifyOnNotificationKey = CBMConnectPeripheralOptionNotifyOnNotificationKey
91+
public let CBConnectPeripheralOptionNotifyOnConnectionKey =
92+
CBMConnectPeripheralOptionNotifyOnConnectionKey
93+
public let CBConnectPeripheralOptionNotifyOnDisconnectionKey =
94+
CBMConnectPeripheralOptionNotifyOnDisconnectionKey
95+
public let CBConnectPeripheralOptionNotifyOnNotificationKey =
96+
CBMConnectPeripheralOptionNotifyOnNotificationKey

Sources/iOS-BLE-Library-Mock/CentralManager/CentralManager.swift

+34-30
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ extension CentralManager {
1818
public var localizedDescription: String {
1919
switch self {
2020
case .wrongManager:
21-
return "Incorrect manager instance provided. Delegate must be of type ReactiveCentralManagerDelegate."
21+
return
22+
"Incorrect manager instance provided. Delegate must be of type ReactiveCentralManagerDelegate."
2223
case .badState(let state):
2324
return "Bad state: \(state)."
2425
case .unknownError:
@@ -54,7 +55,7 @@ private class Observer: NSObject {
5455
}
5556

5657
/// A Custom Central Manager class.
57-
///
58+
///
5859
/// It wraps the standard CBCentralManager and has similar API. However, instead of using delegate, it uses publishers, thus bringing the reactive programming paradigm to the CoreBluetooth framework.
5960
public class CentralManager {
6061
private let isScanningSubject = CurrentValueSubject<Bool, Never>(false)
@@ -63,7 +64,7 @@ public class CentralManager {
6364

6465
/// The underlying CBCentralManager instance.
6566
public let centralManager: CBCentralManager
66-
67+
6768
/// The reactive delegate for the ``centralManager``.
6869
public let centralManagerDelegate: ReactiveCentralManagerDelegate
6970

@@ -73,10 +74,12 @@ public class CentralManager {
7374
/// - queue: The queue to perform operations on. Default is the main queue.
7475
public init(
7576
centralManagerDelegate: ReactiveCentralManagerDelegate =
76-
ReactiveCentralManagerDelegate(), queue: DispatchQueue = .main, options: [String : Any]? = nil
77+
ReactiveCentralManagerDelegate(), queue: DispatchQueue = .main,
78+
options: [String: Any]? = nil
7779
) {
7880
self.centralManagerDelegate = centralManagerDelegate
79-
self.centralManager = CBMCentralManagerFactory.instance(delegate: centralManagerDelegate, queue: queue)
81+
self.centralManager = CBMCentralManagerFactory.instance(
82+
delegate: centralManagerDelegate, queue: queue)
8083
observer.setup()
8184
}
8285

@@ -109,17 +112,17 @@ extension CentralManager {
109112
/// If the peripheral was disconnected successfully, the publisher finishes without error.
110113
/// If the connection was unsuccessful or disconnection returns an error (e.g., peripheral disconnected unexpectedly),
111114
/// the publisher finishes with an error.
112-
///
113-
/// Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
114-
/// The returned publisher will emit the connected peripheral or an error if the connection fails.
115-
/// The publisher will not complete until the peripheral is disconnected.
116-
/// If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.
117-
///
118-
/// ```swift
119-
/// centralManager.connect(peripheral)
120-
/// .sink { completion in
121-
/// switch completion {
122-
/// case .finished:
115+
///
116+
/// Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
117+
/// The returned publisher will emit the connected peripheral or an error if the connection fails.
118+
/// The publisher will not complete until the peripheral is disconnected.
119+
/// If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.
120+
///
121+
/// ```swift
122+
/// centralManager.connect(peripheral)
123+
/// .sink { completion in
124+
/// switch completion {
125+
/// case .finished:
123126
/// print("Peripheral disconnected successfully")
124127
/// case .failure(let error):
125128
/// print("Error: \(error)")
@@ -152,15 +155,16 @@ extension CentralManager {
152155
.bluetooth {
153156
self.centralManager.connect(peripheral, options: options)
154157
}
155-
.autoconnect()
156-
.eraseToAnyPublisher()
158+
.autoconnect()
159+
.eraseToAnyPublisher()
157160
}
158161

159162
/// Cancels the connection with the specified peripheral.
160163
/// - Parameter peripheral: The peripheral to disconnect from.
161164
/// - Returns: A publisher that emits the disconnected peripheral.
162-
public func cancelPeripheralConnection(_ peripheral: CBPeripheral) -> AnyPublisher<CBPeripheral, Error>
163-
{
165+
public func cancelPeripheralConnection(_ peripheral: CBPeripheral) -> AnyPublisher<
166+
CBPeripheral, Error
167+
> {
164168
return self.disconnectedPeripheralsChannel
165169
.tryFilter { r in
166170
guard r.0.identifier == peripheral.identifier else {
@@ -175,17 +179,17 @@ extension CentralManager {
175179
}
176180
.map { $0.0 }
177181
.first()
178-
.bluetooth {
179-
self.centralManager.cancelPeripheralConnection(peripheral)
180-
}
181-
.autoconnect()
182-
.eraseToAnyPublisher()
182+
.bluetooth {
183+
self.centralManager.cancelPeripheralConnection(peripheral)
184+
}
185+
.autoconnect()
186+
.eraseToAnyPublisher()
183187
}
184188
}
185189

186190
// MARK: Retrieving Lists of Peripherals
187191
extension CentralManager {
188-
#warning("check `connect` method")
192+
#warning("check `connect` method")
189193
/// Returns a list of the peripherals connected to the system whose
190194
/// services match a given set of criteria.
191195
///
@@ -218,9 +222,9 @@ extension CentralManager {
218222
extension CentralManager {
219223
#warning("Question: Should we throw an error if the scan is already running?")
220224
/// Initiates a scan for peripherals with the specified services.
221-
///
225+
///
222226
/// Calling this method stops an ongoing scan if it is already running and finishes the publisher returned by ``scanForPeripherals(withServices:)``.
223-
///
227+
///
224228
/// - Parameter services: The services to scan for.
225229
/// - Returns: A publisher that emits scan results or an error.
226230
public func scanForPeripherals(withServices services: [CBUUID]?)
@@ -250,8 +254,8 @@ extension CentralManager {
250254
.bluetooth {
251255
self.centralManager.scanForPeripherals(withServices: services)
252256
}
253-
.autoconnect()
254-
.eraseToAnyPublisher()
257+
.autoconnect()
258+
.eraseToAnyPublisher()
255259
}
256260

257261
/// Stops an ongoing scan for peripherals.

0 commit comments

Comments
 (0)