Skip to content

Commit

Permalink
Cross platform support
Browse files Browse the repository at this point in the history
  • Loading branch information
rhummelmose committed Oct 21, 2016
1 parent 8697018 commit 4a5d888
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 30 deletions.
8 changes: 8 additions & 0 deletions BluetoothKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
91575CE01DBAA7B800445F38 /* CBLegacyManagerState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91575CDF1DBAA7B800445F38 /* CBLegacyManagerState.swift */; };
91575CE11DBAA7B800445F38 /* CBLegacyManagerState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91575CDF1DBAA7B800445F38 /* CBLegacyManagerState.swift */; };
91575CE21DBAA7B800445F38 /* CBLegacyManagerState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91575CDF1DBAA7B800445F38 /* CBLegacyManagerState.swift */; };
C64CDDFB1BE4C56C00F72549 /* BKAvailability.swift in Sources */ = {isa = PBXBuildFile; fileRef = C67A22721B8C80C300CB74CE /* BKAvailability.swift */; };
C64CDDFC1BE4C56C00F72549 /* BKCBCentralManagerDelegateProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = C687E3A61B8B4A2800B702D8 /* BKCBCentralManagerDelegateProxy.swift */; };
C64CDDFD1BE4C56C00F72549 /* BKCBPeripheralDelegateProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = C687E3A71B8B4A2800B702D8 /* BKCBPeripheralDelegateProxy.swift */; };
Expand Down Expand Up @@ -74,6 +77,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
91575CDF1DBAA7B800445F38 /* CBLegacyManagerState.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CBLegacyManagerState.swift; sourceTree = "<group>"; };
C64CDDF31BE4C46A00F72549 /* BluetoothKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = BluetoothKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
C66888AC1B978181003C8DBB /* BKDiscovery.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BKDiscovery.swift; sourceTree = "<group>"; };
C66888AE1B984792003C8DBB /* BKConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BKConfiguration.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -163,6 +167,7 @@
C687E3A61B8B4A2800B702D8 /* BKCBCentralManagerDelegateProxy.swift */,
C687E3A71B8B4A2800B702D8 /* BKCBPeripheralDelegateProxy.swift */,
C67A22771B8C86C800CB74CE /* BKCBPeripheralManagerDelegateProxy.swift */,
91575CDF1DBAA7B800445F38 /* CBLegacyManagerState.swift */,
C687E3A81B8B4A2800B702D8 /* BKCentral.swift */,
C687E3AB1B8B4A2800B702D8 /* BKCentralStateMachine.swift */,
C66888AE1B984792003C8DBB /* BKConfiguration.swift */,
Expand Down Expand Up @@ -386,6 +391,7 @@
C64CDDFC1BE4C56C00F72549 /* BKCBCentralManagerDelegateProxy.swift in Sources */,
C64CDDFD1BE4C56C00F72549 /* BKCBPeripheralDelegateProxy.swift in Sources */,
C64CDDFF1BE4C56C00F72549 /* BKCentral.swift in Sources */,
91575CE21DBAA7B800445F38 /* CBLegacyManagerState.swift in Sources */,
C64CDE001BE4C56C00F72549 /* BKCentralStateMachine.swift in Sources */,
C6B2E3771CE52DD900D25393 /* BKRemotePeer.swift in Sources */,
C64CDE011BE4C56C00F72549 /* BKConfiguration.swift in Sources */,
Expand All @@ -409,6 +415,7 @@
C67A22781B8C86C800CB74CE /* BKCBPeripheralManagerDelegateProxy.swift in Sources */,
C6B2E3711CE52C7700D25393 /* BKErrorDomain.swift in Sources */,
C67A22751B8C83D900CB74CE /* BKPeripheralStateMachine.swift in Sources */,
91575CE01DBAA7B800445F38 /* CBLegacyManagerState.swift in Sources */,
C687E3B81B8B4A2800B702D8 /* BKRemotePeripheral.swift in Sources */,
C67A22361B8C49CC00CB74CE /* BKConnectionAttempt.swift in Sources */,
C6BFB2941B959C8800AB5CAE /* BKContinuousScanner.swift in Sources */,
Expand Down Expand Up @@ -438,6 +445,7 @@
C6ED86C91BA955DB00C9B70B /* BKAvailability.swift in Sources */,
C6B2E3721CE52C7700D25393 /* BKErrorDomain.swift in Sources */,
C6ED86CA1BA955DB00C9B70B /* BKCBCentralManagerDelegateProxy.swift in Sources */,
91575CE11DBAA7B800445F38 /* CBLegacyManagerState.swift in Sources */,
C6ED86CB1BA955DB00C9B70B /* BKCBPeripheralDelegateProxy.swift in Sources */,
C6ED86CC1BA955DB00C9B70B /* BKCBPeripheralManagerDelegateProxy.swift in Sources */,
C6ED86CD1BA955DB00C9B70B /* BKCentral.swift in Sources */,
Expand Down
8 changes: 6 additions & 2 deletions Source/BKAvailability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public enum BKAvailability: Equatable {
case available
case unavailable(cause: BKUnavailabilityCause)

#if os(iOS) || os(tvOS)
@available(iOS 10.0, tvOS 10.0, *)
@available(OSX, unavailable)
internal init(managerState: CBManagerState) {
Expand All @@ -55,7 +56,8 @@ public enum BKAvailability: Equatable {
default: self = .unavailable(cause: BKUnavailabilityCause(managerState: managerState))
}
}

#endif

internal init(centralManagerState: CBCentralManagerState) {
switch centralManagerState {
case .poweredOn: self = .available
Expand Down Expand Up @@ -91,6 +93,7 @@ public enum BKUnavailabilityCause: ExpressibleByNilLiteral {
self = .any
}

#if os(iOS) || os(tvOS)
@available(iOS 10.0, tvOS 10.0, *)
@available(OSX, unavailable)
internal init(managerState: CBManagerState) {
Expand All @@ -102,7 +105,8 @@ public enum BKUnavailabilityCause: ExpressibleByNilLiteral {
default: self = nil
}
}

#endif

internal init(centralManagerState: CBCentralManagerState) {
switch centralManagerState {
case .poweredOff: self = .poweredOff
Expand Down
31 changes: 19 additions & 12 deletions Source/BKCentral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
guard let centralManager = _centralManager else {
return nil
}
if #available(iOS 10.0, tvOS 10.0, *) {
return BKAvailability(managerState: centralManager.state)
} else {
return BKAvailability(centralManagerState: CBCentralManagerState(rawValue: centralManager.state.rawValue)!)
}
#if os(iOS) || os(tvOS)
if #available(tvOS 10.0, iOS 10.0, *) {
return BKAvailability(managerState: centralManager.state)
} else {
return BKAvailability(centralManagerState: centralManager.centralManagerState)
}
#else
return BKAvailability(centralManagerState: centralManager.state)
#endif

}

/// All currently connected remote peripherals.
Expand Down Expand Up @@ -337,13 +342,15 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
break
case .unsupported, .unauthorized, .poweredOff:
let newCause: BKUnavailabilityCause

if #available(iOS 10.0, *), #available(tvOS 10.0, *) {
newCause = BKUnavailabilityCause(managerState: central.state)
} else {
newCause = BKUnavailabilityCause(centralManagerState: CBCentralManagerState(rawValue: central.state.rawValue)!)
}

#if os(iOS) || os(tvOS)
if #available(iOS 10.0, tvOS 10.0, *) {
newCause = BKUnavailabilityCause(managerState: central.state)
} else {
newCause = BKUnavailabilityCause(centralManagerState: central.centralManagerState)
}
#else
newCause = BKUnavailabilityCause(centralManagerState: central.state)
#endif
switch stateMachine.state {
case let .unavailable(cause):
let oldCause = cause
Expand Down
31 changes: 18 additions & 13 deletions Source/BKPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
/// Bluetooth LE availability derived from the underlying CBPeripheralManager object.

public var availability: BKAvailability {
if #available(iOS 10.0, *), #available(tvOS 10.0, *) {
return BKAvailability(managerState: peripheralManager.state)
} else {
return BKAvailability(peripheralManagerState: CBPeripheralManagerState(rawValue:peripheralManager.state.rawValue)!)
}
#if os(iOS) || os(tvOS)
if #available(iOS 10.0, tvOS 10.0, *) {
return BKAvailability(managerState: peripheralManager.state)
} else {
return BKAvailability(peripheralManagerState: peripheralManager.peripheralManagerState)
}
#else
return BKAvailability(peripheralManagerState: peripheralManager.state)
#endif
}


Expand Down Expand Up @@ -189,15 +193,16 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
case .unknown, .resetting:
break
case .unsupported, .unauthorized, .poweredOff:

let newCause: BKUnavailabilityCause
if #available(iOS 10.0, *), #available(tvOS 10.0, *) {
newCause = BKUnavailabilityCause(managerState: peripheralManager.state)
} else {
newCause = BKUnavailabilityCause(peripheralManagerState: CBPeripheralManagerState(rawValue: peripheralManager.state.rawValue)!)
}


#if os(iOS) || os(tvOS)
if #available(iOS 10.0, tvOS 10.0, *) {
newCause = BKUnavailabilityCause(managerState: peripheralManager.state)
} else {
newCause = BKUnavailabilityCause(peripheralManagerState: peripheralManager.peripheralManagerState)
}
#else
newCause = BKUnavailabilityCause(peripheralManagerState: peripheralManager.state)
#endif
switch stateMachine.state {
case let .unavailable(cause):
let oldCause = cause
Expand Down
6 changes: 3 additions & 3 deletions Source/BKRemotePeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public class BKRemotePeripheral: BKRemotePeer, BKCBPeripheralDelegate {
}
#else
switch peripheral!.state {
case .Disconnected: return .Disconnected
case .Connecting: return .Connecting
case .Connected: return .Connected
case .disconnected: return .disconnected
case .connecting: return .connecting
case .connected: return .connected
}
#endif
}
Expand Down
30 changes: 30 additions & 0 deletions Source/CBLegacyManagerState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// CBLegacyManagerState.swift
// BluetoothKit
//
// Created by Rasmus H. Hummelmose on 21/10/2016.
// Copyright © 2016 Rasmus Taulborg Hummelmose. All rights reserved.
//

import Foundation
import CoreBluetooth

extension CBCentralManager {

internal var centralManagerState: CBCentralManagerState {
get {
return CBCentralManagerState(rawValue: state.rawValue) ?? .unknown
}
}

}

extension CBPeripheralManager {

internal var peripheralManagerState: CBPeripheralManagerState {
get {
return CBPeripheralManagerState(rawValue: state.rawValue) ?? .unknown
}
}

}

0 comments on commit 4a5d888

Please sign in to comment.