Skip to content

Commit

Permalink
Merge pull request rhummelmose#100 from ricobeck/master
Browse files Browse the repository at this point in the history
Adds Swift Package Manager support
  • Loading branch information
rhummelmose authored Mar 1, 2021
2 parents 4958fff + 5ae7f3a commit bddd4b3
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 52 deletions.
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion BluetoothKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Pod::Spec.new do |s|
s.authors = { 'Rasmus Taulborg Hummelmose' => '[email protected]' }
s.summary = 'Easily communicate between iOS devices using BLE.'
s.source = { :git => 'https://github.com/rasmusth/BluetoothKit.git', :tag => "#{s.version}" }
s.source_files = 'Source/**/*.{swift}', 'Umbrella/**/*.{h}'
s.source_files = 'Sources/BluetoothKit/**/*.{swift}', 'Umbrella/**/*.{h}'
s.requires_arc = true
s.osx.deployment_target = '10.10'
s.ios.deployment_target = '8.0'
Expand Down
6 changes: 6 additions & 0 deletions BluetoothKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 0.4.1;
PRODUCT_BUNDLE_IDENTIFIER = com.rasmusth.BluetoothKit;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
Expand All @@ -490,6 +491,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 0.4.1;
PRODUCT_BUNDLE_IDENTIFIER = com.rasmusth.BluetoothKit;
SDKROOT = appletvos;
SKIP_INSTALL = YES;
Expand Down Expand Up @@ -627,6 +629,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 0.4.1;
PRODUCT_BUNDLE_IDENTIFIER = com.rasmusth.BluetoothKit;
SKIP_INSTALL = YES;
SWIFT_VERSION = 5.0;
Expand All @@ -643,6 +646,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
MARKETING_VERSION = 0.4.1;
PRODUCT_BUNDLE_IDENTIFIER = com.rasmusth.BluetoothKit;
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand All @@ -663,6 +667,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 0.4.1;
PRODUCT_BUNDLE_IDENTIFIER = com.rasmusth.BluetoothKit;
SDKROOT = macosx;
SKIP_INSTALL = YES;
Expand All @@ -683,6 +688,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
MACOSX_DEPLOYMENT_TARGET = 10.13;
MARKETING_VERSION = 0.4.1;
PRODUCT_BUNDLE_IDENTIFIER = com.rasmusth.BluetoothKit;
SDKROOT = macosx;
SKIP_INSTALL = YES;
Expand Down
33 changes: 33 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "BluetoothKit",
platforms: [
.iOS(.v10),
.macOS(.v10_10),
.tvOS(.v9)
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "BluetoothKit",
targets: ["BluetoothKit"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "BluetoothKit",
dependencies: []),
.testTarget(
name: "BluetoothKitTests",
dependencies: ["BluetoothKit"]),
]
)
2 changes: 1 addition & 1 deletion Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>0.4.0</string>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@ import CoreBluetooth

public func == (lhs: BKAvailability, rhs: BKAvailability) -> Bool {
switch (lhs, rhs) {
case (.available, .available): return true
case (.unavailable(cause: .any), .unavailable): return true
case (.unavailable, .unavailable(cause: .any)): return true
case (.unavailable(let lhsCause), .unavailable(let rhsCause)): return lhsCause == rhsCause
default: return false
case (.available, .available):
return true
case (.unavailable(cause: .any), .unavailable):
return true
case (.unavailable, .unavailable(cause: .any)):
return true
case (.unavailable(let lhsCause), .unavailable(let rhsCause)):
return lhsCause == rhsCause
default:
return false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal protocol BKCBCentralManagerStateDelegate: class {
}

internal protocol BKCBCentralManagerDiscoveryDelegate: class {
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber)
}

internal protocol BKCBCentralManagerConnectionDelegate: class {
Expand Down Expand Up @@ -62,7 +62,7 @@ internal class BKCBCentralManagerDelegateProxy: NSObject, CBCentralManagerDelega
stateDelegate?.centralManagerDidUpdateState(central)
}

internal func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
internal func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String: Any], rssi RSSI: NSNumber) {
discoveryDelegate?.centralManager(central, didDiscover: peripheral, advertisementData: advertisementData, rssi: RSSI)
}

Expand All @@ -78,8 +78,4 @@ internal class BKCBCentralManagerDelegateProxy: NSObject, CBCentralManagerDelega
connectionDelegate?.centralManager(central, didDisconnectPeripheral: peripheral, error: error)
}





}
File renamed without changes.
13 changes: 6 additions & 7 deletions Source/BKCentral.swift → Sources/BluetoothKit/BKCentral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
guard let centralManager = _centralManager else {
return nil
}

return BKAvailability(managerState: centralManager.state)
}

Expand Down Expand Up @@ -113,15 +113,15 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
private let connectionPool = BKConnectionPool()
private var _configuration: BKConfiguration?
private var continuousScanner: BKContinousScanner!
private var centralManagerDelegate: BKCBCentralManagerDelegateProxy!
private var centralManagerDelegateProxy: BKCBCentralManagerDelegateProxy!
private var stateMachine: BKCentralStateMachine!
private var _centralManager: CBCentralManager!

// MARK: Initialization

public override init() {
super.init()
centralManagerDelegate = BKCBCentralManagerDelegateProxy(stateDelegate: self, discoveryDelegate: scanner, connectionDelegate: connectionPool)
centralManagerDelegateProxy = BKCBCentralManagerDelegateProxy(stateDelegate: self, discoveryDelegate: scanner, connectionDelegate: connectionPool)
stateMachine = BKCentralStateMachine()
connectionPool.delegate = self
continuousScanner = BKContinousScanner(scanner: scanner)
Expand All @@ -138,7 +138,7 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
do {
try stateMachine.handleEvent(.start)
_configuration = configuration
_centralManager = CBCentralManager(delegate: centralManagerDelegate, queue: nil, options: nil)
_centralManager = CBCentralManager(delegate: centralManagerDelegateProxy, queue: nil, options: nil)
scanner.configuration = configuration
scanner.centralManager = centralManager
connectionPool.centralManager = centralManager
Expand All @@ -150,7 +150,7 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
/**
Scan for peripherals for a limited duration of time.
- parameter duration: The number of seconds to scan for (defaults to 3). A duration of 0 means endless
- parameter updateDuplicates: normally, discoveries for the same peripheral are coalesced by IOS. Setting this to true advises the OS to generate new discoveries anyway. This allows you to react to RSSI changes (defaults to false).
- parameter updateDuplicates: normally, discoveries for the same peripheral are coalesced by iOS. Setting this to true advises the OS to generate new discoveries anyway. This allows you to react to RSSI changes (defaults to false).
- parameter progressHandler: A progress handler allowing you to react immediately when a peripheral is discovered during a scan.
- parameter completionHandler: A completion handler allowing you to react on the full result of discovered peripherals or an error if one occured.
*/
Expand Down Expand Up @@ -326,7 +326,6 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo

// MARK: BKCBCentralManagerStateDelegate


internal func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unknown, .resetting:
Expand All @@ -343,7 +342,7 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
_ = try? stateMachine.handleEvent(.setUnavailable(cause: newCause))
setUnavailable(newCause, oldCause: nil)
}

case .poweredOn:
let state = stateMachine.state
_ = try? stateMachine.handleEvent(.setAvailable)
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class BKConfiguration {
dataCancelledMark = "COD".data(using: String.Encoding.utf8)!
}

// MARK Functions
// MARK: Functions

internal func characteristicUUIDsForServiceUUID(_ serviceUUID: CBUUID) -> [CBUUID] {
if serviceUUID == dataServiceUUID {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ internal class BKContinousScanner {
state = .stopped
}

// MARK Internal Functions
// MARK: Internal Functions

internal func scanContinuouslyWithChangeHandler(_ changeHandler: @escaping ChangeHandler, stateHandler: StateHandler? = nil, duration: TimeInterval = 3, inBetweenDelay: TimeInterval = 3,updateDuplicates: Bool, errorHandler: ErrorHandler?) {
internal func scanContinuouslyWithChangeHandler(_ changeHandler: @escaping ChangeHandler, stateHandler: StateHandler? = nil, duration: TimeInterval = 3, inBetweenDelay: TimeInterval = 3, updateDuplicates: Bool, errorHandler: ErrorHandler?) {
guard !busy else {
errorHandler?(.busy)
return
Expand Down Expand Up @@ -91,24 +91,23 @@ internal class BKContinousScanner {
stateHandler?(state)
try scanner.scanWithDuration(duration, updateDuplicates: self.updateDuplicates, progressHandler: { newDiscoveries in
var changes: [BKDiscoveriesChange] = []

//find discoveries that have been updated and add a change for each
for newDiscovery in newDiscoveries {
if let index = self.maintainedDiscoveries.firstIndex(of: newDiscovery) {
let outdatedDiscovery = self.maintainedDiscoveries[index]
self.maintainedDiscoveries[index] = newDiscovery

//TODO: probably need an update change
changes.append(.remove(discovery: outdatedDiscovery))
changes.append(.insert(discovery: newDiscovery))
}
else if !self.maintainedDiscoveries.contains(newDiscovery) {

} else if !self.maintainedDiscoveries.contains(newDiscovery) {

self.maintainedDiscoveries.append(newDiscovery)
changes.append(.insert(discovery: newDiscovery))
}
}

if !changes.isEmpty {
self.changeHandler?(changes, self.maintainedDiscoveries)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ import Foundation

public func == (lhs: BKDiscoveriesChange, rhs: BKDiscoveriesChange) -> Bool {
switch (lhs, rhs) {
case (.insert(let lhsDiscovery), .insert(let rhsDiscovery)): return lhsDiscovery == rhsDiscovery || lhsDiscovery == nil || rhsDiscovery == nil
case (.remove(let lhsDiscovery), .remove(let rhsDiscovery)): return lhsDiscovery == rhsDiscovery || lhsDiscovery == nil || rhsDiscovery == nil
default: return false
case (.insert(let lhsDiscovery), .insert(let rhsDiscovery)):
return lhsDiscovery == rhsDiscovery || lhsDiscovery == nil || rhsDiscovery == nil
case (.remove(let lhsDiscovery), .remove(let rhsDiscovery)):
return lhsDiscovery == rhsDiscovery || lhsDiscovery == nil || rhsDiscovery == nil
default:
return false
}
}

Expand All @@ -47,8 +50,10 @@ public enum BKDiscoveriesChange: Equatable {
/// The discovery associated with the change.
public var discovery: BKDiscovery! {
switch self {
case .insert(let discovery): return discovery
case .remove(let discovery): return discovery
case .insert(let discovery):
return discovery
case .remove(let discovery):
return discovery
}
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
return BKAvailability(managerState: peripheralManager.state)
}



/// The configuration that the BKPeripheral object was started with.
override public var configuration: BKPeripheralConfiguration? {
return _configuration
Expand All @@ -82,15 +80,15 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
private var _configuration: BKPeripheralConfiguration!
private var peripheralManager: CBPeripheralManager!
private let stateMachine = BKPeripheralStateMachine()
private var peripheralManagerDelegate: BKCBPeripheralManagerDelegateProxy!
private var peripheralManagerDelegateProxy: BKCBPeripheralManagerDelegateProxy!
private var characteristicData: CBMutableCharacteristic!
private var dataService: CBMutableService!

// MARK: Initialization

public override init() {
super.init()
peripheralManagerDelegate = BKCBPeripheralManagerDelegateProxy(delegate: self)
peripheralManagerDelegateProxy = BKCBPeripheralManagerDelegateProxy(delegate: self)
}

// MARK: Public Functions
Expand All @@ -105,7 +103,7 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
do {
try stateMachine.handleEvent(event: .start)
_configuration = configuration
peripheralManager = CBPeripheralManager(delegate: peripheralManagerDelegate, queue: nil, options: nil)
peripheralManager = CBPeripheralManager(delegate: peripheralManagerDelegateProxy, queue: nil, options: nil)
} catch let error {
throw BKError.internalError(underlyingError: error)
}
Expand Down Expand Up @@ -183,7 +181,7 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
break
case .unsupported, .unauthorized, .poweredOff:
let newCause = BKUnavailabilityCause(managerState: peripheralManager.state)

switch stateMachine.state {
case let .unavailable(cause):
let oldCause = cause
Expand All @@ -207,7 +205,6 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
}
}


internal func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {

}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ public class BKRemotePeripheral: BKRemotePeer, BKCBPeripheralDelegate {
return .shallow
}
switch peripheral!.state {
case .disconnected: return .disconnected
case .connecting: return .connecting
case .connected: return .connected
case .disconnecting: return .disconnecting
@unknown default: return .shallow

case .disconnected:
return .disconnected
case .connecting:
return .connecting
case .connected:
return .connected
case .disconnecting:
return .disconnecting
@unknown default:
return .shallow

}
}

Expand Down Expand Up @@ -176,5 +181,4 @@ public class BKRemotePeripheral: BKRemotePeer, BKCBPeripheralDelegate {
handleReceivedData(characteristic.value!)
}


}
Loading

0 comments on commit bddd4b3

Please sign in to comment.