Skip to content

Commit

Permalink
Copied files from native CoreBluetooth version to CoreBluetoothMock
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKibish authored and github-actions[bot] committed Oct 6, 2023
1 parent f312984 commit 0f899bc
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 165 deletions.
87 changes: 48 additions & 39 deletions Sources/iOS-BLE-Library-Mock/Alias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,50 +38,59 @@ import CoreBluetoothMock
// disabled for Xcode 12.5 beta
//typealias CBPeer = CBMPeer
//typealias CBAttribute = CBMAttribute
public typealias CBCentralManagerFactory = CBMCentralManagerFactory
public typealias CBUUID = CBMUUID
public typealias CBError = CBMError
public typealias CBATTError = CBMATTError
public typealias CBManagerState = CBMManagerState
public typealias CBPeripheralState = CBMPeripheralState
public typealias CBCentralManager = CBMCentralManager
public typealias CBCentralManagerDelegate = CBMCentralManagerDelegate
public typealias CBPeripheral = CBMPeripheral
public typealias CBPeripheralDelegate = CBMPeripheralDelegate
public typealias CBService = CBMService
public typealias CBCharacteristic = CBMCharacteristic
public typealias CBCharacteristicWriteType = CBMCharacteristicWriteType
public typealias CBCharacteristicProperties = CBMCharacteristicProperties
public typealias CBDescriptor = CBMDescriptor
public typealias CBConnectionEvent = CBMConnectionEvent
public typealias CBCentralManagerFactory = CBMCentralManagerFactory
public typealias CBUUID = CBMUUID
public typealias CBError = CBMError
public typealias CBATTError = CBMATTError
public typealias CBManagerState = CBMManagerState
public typealias CBPeripheralState = CBMPeripheralState
public typealias CBCentralManager = CBMCentralManager
public typealias CBCentralManagerDelegate = CBMCentralManagerDelegate
public typealias CBPeripheral = CBMPeripheral
public typealias CBPeripheralDelegate = CBMPeripheralDelegate
public typealias CBService = CBMService
public typealias CBCharacteristic = CBMCharacteristic
public typealias CBCharacteristicWriteType = CBMCharacteristicWriteType
public typealias CBCharacteristicProperties = CBMCharacteristicProperties
public typealias CBDescriptor = CBMDescriptor
public typealias CBConnectionEvent = CBMConnectionEvent
public typealias CBConnectionEventMatchingOption = CBMConnectionEventMatchingOption
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, *)
public typealias CBL2CAPPSM = CBML2CAPPSM
public typealias CBL2CAPPSM = CBML2CAPPSM
@available(iOS 11.0, tvOS 11.0, watchOS 4.0, *)
public typealias CBL2CAPChannel = CBML2CAPChannel
public typealias CBL2CAPChannel = CBML2CAPChannel

public let CBCentralManagerScanOptionAllowDuplicatesKey = CBMCentralManagerScanOptionAllowDuplicatesKey
public let CBCentralManagerOptionShowPowerAlertKey = CBMCentralManagerOptionShowPowerAlertKey
public let CBCentralManagerOptionRestoreIdentifierKey = CBMCentralManagerOptionRestoreIdentifierKey
public let CBCentralManagerScanOptionSolicitedServiceUUIDsKey = CBMCentralManagerScanOptionSolicitedServiceUUIDsKey
public let CBConnectPeripheralOptionStartDelayKey = CBMConnectPeripheralOptionStartDelayKey
public let CBCentralManagerScanOptionAllowDuplicatesKey =
CBMCentralManagerScanOptionAllowDuplicatesKey
public let CBCentralManagerOptionShowPowerAlertKey = CBMCentralManagerOptionShowPowerAlertKey
public let CBCentralManagerOptionRestoreIdentifierKey = CBMCentralManagerOptionRestoreIdentifierKey
public let CBCentralManagerScanOptionSolicitedServiceUUIDsKey =
CBMCentralManagerScanOptionSolicitedServiceUUIDsKey
public let CBConnectPeripheralOptionStartDelayKey = CBMConnectPeripheralOptionStartDelayKey
#if !os(macOS)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public let CBConnectPeripheralOptionRequiresANCS = CBMConnectPeripheralOptionRequiresANCS
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)
public let CBConnectPeripheralOptionRequiresANCS = CBMConnectPeripheralOptionRequiresANCS
#endif
public let CBCentralManagerRestoredStatePeripheralsKey = CBMCentralManagerRestoredStatePeripheralsKey
public let CBCentralManagerRestoredStateScanServicesKey = CBMCentralManagerRestoredStateScanServicesKey
public let CBCentralManagerRestoredStateScanOptionsKey = CBMCentralManagerRestoredStateScanOptionsKey
public let CBCentralManagerRestoredStatePeripheralsKey =
CBMCentralManagerRestoredStatePeripheralsKey
public let CBCentralManagerRestoredStateScanServicesKey =
CBMCentralManagerRestoredStateScanServicesKey
public let CBCentralManagerRestoredStateScanOptionsKey =
CBMCentralManagerRestoredStateScanOptionsKey

public let CBAdvertisementDataLocalNameKey = CBMAdvertisementDataLocalNameKey
public let CBAdvertisementDataServiceUUIDsKey = CBMAdvertisementDataServiceUUIDsKey
public let CBAdvertisementDataIsConnectable = CBMAdvertisementDataIsConnectable
public let CBAdvertisementDataTxPowerLevelKey = CBMAdvertisementDataTxPowerLevelKey
public let CBAdvertisementDataServiceDataKey = CBMAdvertisementDataServiceDataKey
public let CBAdvertisementDataManufacturerDataKey = CBMAdvertisementDataManufacturerDataKey
public let CBAdvertisementDataOverflowServiceUUIDsKey = CBMAdvertisementDataOverflowServiceUUIDsKey
public let CBAdvertisementDataSolicitedServiceUUIDsKey = CBMAdvertisementDataSolicitedServiceUUIDsKey
public let CBAdvertisementDataLocalNameKey = CBMAdvertisementDataLocalNameKey
public let CBAdvertisementDataServiceUUIDsKey = CBMAdvertisementDataServiceUUIDsKey
public let CBAdvertisementDataIsConnectable = CBMAdvertisementDataIsConnectable
public let CBAdvertisementDataTxPowerLevelKey = CBMAdvertisementDataTxPowerLevelKey
public let CBAdvertisementDataServiceDataKey = CBMAdvertisementDataServiceDataKey
public let CBAdvertisementDataManufacturerDataKey = CBMAdvertisementDataManufacturerDataKey
public let CBAdvertisementDataOverflowServiceUUIDsKey = CBMAdvertisementDataOverflowServiceUUIDsKey
public let CBAdvertisementDataSolicitedServiceUUIDsKey =
CBMAdvertisementDataSolicitedServiceUUIDsKey

public let CBConnectPeripheralOptionNotifyOnConnectionKey = CBMConnectPeripheralOptionNotifyOnConnectionKey
public let CBConnectPeripheralOptionNotifyOnDisconnectionKey = CBMConnectPeripheralOptionNotifyOnDisconnectionKey
public let CBConnectPeripheralOptionNotifyOnNotificationKey = CBMConnectPeripheralOptionNotifyOnNotificationKey
public let CBConnectPeripheralOptionNotifyOnConnectionKey =
CBMConnectPeripheralOptionNotifyOnConnectionKey
public let CBConnectPeripheralOptionNotifyOnDisconnectionKey =
CBMConnectPeripheralOptionNotifyOnDisconnectionKey
public let CBConnectPeripheralOptionNotifyOnNotificationKey =
CBMConnectPeripheralOptionNotifyOnNotificationKey
64 changes: 34 additions & 30 deletions Sources/iOS-BLE-Library-Mock/CentralManager/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ extension CentralManager {
public var localizedDescription: String {
switch self {
case .wrongManager:
return "Incorrect manager instance provided. Delegate must be of type ReactiveCentralManagerDelegate."
return
"Incorrect manager instance provided. Delegate must be of type ReactiveCentralManagerDelegate."
case .badState(let state):
return "Bad state: \(state)."
case .unknownError:
Expand Down Expand Up @@ -54,7 +55,7 @@ private class Observer: NSObject {
}

/// A Custom Central Manager class.
///
///
/// 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.
public class CentralManager {
private let isScanningSubject = CurrentValueSubject<Bool, Never>(false)
Expand All @@ -63,7 +64,7 @@ public class CentralManager {

/// The underlying CBCentralManager instance.
public let centralManager: CBCentralManager

/// The reactive delegate for the ``centralManager``.
public let centralManagerDelegate: ReactiveCentralManagerDelegate

Expand All @@ -73,10 +74,12 @@ public class CentralManager {
/// - queue: The queue to perform operations on. Default is the main queue.
public init(
centralManagerDelegate: ReactiveCentralManagerDelegate =
ReactiveCentralManagerDelegate(), queue: DispatchQueue = .main, options: [String : Any]? = nil
ReactiveCentralManagerDelegate(), queue: DispatchQueue = .main,
options: [String: Any]? = nil
) {
self.centralManagerDelegate = centralManagerDelegate
self.centralManager = CBMCentralManagerFactory.instance(delegate: centralManagerDelegate, queue: queue)
self.centralManager = CBMCentralManagerFactory.instance(
delegate: centralManagerDelegate, queue: queue)
observer.setup()
}

Expand Down Expand Up @@ -109,17 +112,17 @@ extension CentralManager {
/// If the peripheral was disconnected successfully, the publisher finishes without error.
/// If the connection was unsuccessful or disconnection returns an error (e.g., peripheral disconnected unexpectedly),
/// the publisher finishes with an error.
///
/// Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
/// The returned publisher will emit the connected peripheral or an error if the connection fails.
/// The publisher will not complete until the peripheral is disconnected.
/// If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.
///
/// ```swift
/// centralManager.connect(peripheral)
/// .sink { completion in
/// switch completion {
/// case .finished:
///
/// Use ``CentralManager/connect(_:options:)`` to connect to a peripheral.
/// The returned publisher will emit the connected peripheral or an error if the connection fails.
/// The publisher will not complete until the peripheral is disconnected.
/// If the connection fails, or the peripheral is unexpectedly disconnected, the publisher will fail with an error.
///
/// ```swift
/// centralManager.connect(peripheral)
/// .sink { completion in
/// switch completion {
/// case .finished:
/// print("Peripheral disconnected successfully")
/// case .failure(let error):
/// print("Error: \(error)")
Expand Down Expand Up @@ -152,15 +155,16 @@ extension CentralManager {
.bluetooth {
self.centralManager.connect(peripheral, options: options)
}
.autoconnect()
.eraseToAnyPublisher()
.autoconnect()
.eraseToAnyPublisher()
}

/// Cancels the connection with the specified peripheral.
/// - Parameter peripheral: The peripheral to disconnect from.
/// - Returns: A publisher that emits the disconnected peripheral.
public func cancelPeripheralConnection(_ peripheral: CBPeripheral) -> AnyPublisher<CBPeripheral, Error>
{
public func cancelPeripheralConnection(_ peripheral: CBPeripheral) -> AnyPublisher<
CBPeripheral, Error
> {
return self.disconnectedPeripheralsChannel
.tryFilter { r in
guard r.0.identifier == peripheral.identifier else {
Expand All @@ -175,17 +179,17 @@ extension CentralManager {
}
.map { $0.0 }
.first()
.bluetooth {
self.centralManager.cancelPeripheralConnection(peripheral)
}
.autoconnect()
.eraseToAnyPublisher()
.bluetooth {
self.centralManager.cancelPeripheralConnection(peripheral)
}
.autoconnect()
.eraseToAnyPublisher()
}
}

// MARK: Retrieving Lists of Peripherals
extension CentralManager {
#warning("check `connect` method")
#warning("check `connect` method")
/// Returns a list of the peripherals connected to the system whose
/// services match a given set of criteria.
///
Expand Down Expand Up @@ -218,9 +222,9 @@ extension CentralManager {
extension CentralManager {
#warning("Question: Should we throw an error if the scan is already running?")
/// Initiates a scan for peripherals with the specified services.
///
///
/// Calling this method stops an ongoing scan if it is already running and finishes the publisher returned by ``scanForPeripherals(withServices:)``.
///
///
/// - Parameter services: The services to scan for.
/// - Returns: A publisher that emits scan results or an error.
public func scanForPeripherals(withServices services: [CBUUID]?)
Expand Down Expand Up @@ -250,8 +254,8 @@ extension CentralManager {
.bluetooth {
self.centralManager.scanForPeripherals(withServices: services)
}
.autoconnect()
.eraseToAnyPublisher()
.autoconnect()
.eraseToAnyPublisher()
}

/// Stops an ongoing scan for peripherals.
Expand Down
Loading

0 comments on commit 0f899bc

Please sign in to comment.