Skip to content

Commit

Permalink
Merge branch 'release/0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
NickKibish committed Oct 6, 2023
2 parents 9891a1f + 04ca5e6 commit f312984
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 310 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Run copy script
run: |
python3 code_gen/code_gen.py Sources
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "iOS-BLE-Library-Mock"
BuildableName = "iOS-BLE-Library-Mock"
BlueprintName = "iOS-BLE-Library-Mock"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "iOS-BLE-Library-Mock"
BuildableName = "iOS-BLE-Library-Mock"
BlueprintName = "iOS-BLE-Library-Mock"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1500"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "iOS-BLE-LibraryTests"
BuildableName = "iOS-BLE-LibraryTests"
BlueprintName = "iOS-BLE-LibraryTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
87 changes: 39 additions & 48 deletions Sources/iOS-BLE-Library-Mock/Alias.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,59 +38,50 @@ 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
58 changes: 40 additions & 18 deletions Sources/iOS-BLE-Library-Mock/CentralManager/CentralManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ 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 @@ -55,7 +54,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 @@ -64,7 +63,7 @@ public class CentralManager {

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

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

Expand All @@ -74,12 +73,10 @@ 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 @@ -112,8 +109,28 @@ 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:
/// print("Peripheral disconnected successfully")
/// case .failure(let error):
/// print("Error: \(error)")
/// }
/// } receiveValue: { peripheral in
/// print("Peripheral connected: \(peripheral)")
/// }
/// .store(in: &cancellables)
/// ```
public func connect(_ peripheral: CBPeripheral, options: [String: Any]? = nil)
-> Publishers.BluetoothPublisher<CBPeripheral, Error>
-> AnyPublisher<CBPeripheral, Error>
{
let killSwitch = self.disconnectedPeripheralsChannel.tryFirst(where: { p in
if let e = p.1 {
Expand All @@ -135,13 +152,14 @@ extension CentralManager {
.bluetooth {
self.centralManager.connect(peripheral, options: options)
}
.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)
-> Publishers.BluetoothPublisher<CBPeripheral, Error>
public func cancelPeripheralConnection(_ peripheral: CBPeripheral) -> AnyPublisher<CBPeripheral, Error>
{
return self.disconnectedPeripheralsChannel
.tryFilter { r in
Expand All @@ -157,15 +175,17 @@ extension CentralManager {
}
.map { $0.0 }
.first()
.bluetooth {
self.centralManager.cancelPeripheralConnection(peripheral)
}
.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 @@ -198,13 +218,13 @@ 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]?)
-> Publishers.BluetoothPublisher<ScanResult, Error>
-> AnyPublisher<ScanResult, Error>
{
stopScan()
return centralManagerDelegate.stateSubject
Expand All @@ -230,6 +250,8 @@ extension CentralManager {
.bluetooth {
self.centralManager.scanForPeripherals(withServices: services)
}
.autoconnect()
.eraseToAnyPublisher()
}

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

0 comments on commit f312984

Please sign in to comment.