Skip to content

Commit

Permalink
IMPORTANT: Libary will not work without these changes
Browse files Browse the repository at this point in the history
allow ios 8 & 9

By switching on the iOS version we can decide between using the new CBManager.state or the CBPeripheralManager.state or CBCentralManager.state, these states are all compatible with each other and still convert into an Availability and UnavailabiltyCause

fix delegate BKCBPeripheralManagerDelegateProxy so it conforms properly to CBPeripheralManagerDelegate and recieves calls, (NSError -> Error, AnyObject -> Any).
remove unused didRestoreState to fix warning.
  • Loading branch information
David Rees committed Oct 18, 2016
1 parent cd472bb commit cf860ba
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 67 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added Source/.DS_Store
Binary file not shown.
48 changes: 31 additions & 17 deletions Source/BKAvailability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,26 @@ public enum BKAvailability: Equatable {
case unavailable(cause: BKUnavailabilityCause)

@available(iOS 10.0, *)
internal init(centralManagerState: CBManagerState) {
switch centralManagerState {
internal init(centralState: CBManagerState) {
switch centralState {
case .poweredOn: self = .available
default: self = .unavailable(cause: BKUnavailabilityCause(centralManagerState: centralManagerState))
default: self = .unavailable(cause: BKUnavailabilityCause(centralState: centralState))
}
}

@available(iOS 10.0, *)
internal init(peripheralManagerState: CBManagerState) {
internal init(centralManagerState: CBCentralManagerState){
switch centralManagerState {
case .poweredOn: self = .available
default: self = .unavailable(cause: BKUnavailabilityCause(centralManagerState: centralManagerState))
}
}

internal init(peripheralManagerState: CBPeripheralManagerState){
switch peripheralManagerState {
case .poweredOn: self = .available
default: self = .unavailable(cause: BKUnavailabilityCause(peripheralManagerState: peripheralManagerState))
case .poweredOn: self = .available
default: self = .unavailable(cause: BKUnavailabilityCause(peripheralManagerState: peripheralManagerState))
}
}

}

/**
Expand All @@ -86,8 +91,8 @@ public enum BKUnavailabilityCause: NilLiteralConvertible {
}

@available(iOS 10.0, *)
internal init(centralManagerState: CBManagerState) {
switch centralManagerState {
internal init(centralState: CBManagerState) {
switch centralState {
case .poweredOff: self = .poweredOff
case .resetting: self = .resetting
case .unauthorized: self = .unauthorized
Expand All @@ -96,14 +101,23 @@ public enum BKUnavailabilityCause: NilLiteralConvertible {
}
}

@available(iOS 10.0, *)
internal init(peripheralManagerState: CBManagerState) {
internal init(centralManagerState: CBCentralManagerState) {
switch centralManagerState {
case .poweredOff: self = .poweredOff
case .resetting: self = .resetting
case .unauthorized: self = .unauthorized
case .unsupported: self = .unsupported
default: self = nil
}
}

internal init(peripheralManagerState: CBPeripheralManagerState) {
switch peripheralManagerState {
case .poweredOff: self = .poweredOff
case .resetting: self = .resetting
case .unauthorized: self = .unauthorized
case .unsupported: self = .unsupported
default: self = nil
case .poweredOff: self = .poweredOff
case .resetting: self = .resetting
case .unauthorized: self = .unauthorized
case .unsupported: self = .unsupported
default: self = nil
}
}

Expand Down
27 changes: 16 additions & 11 deletions Source/BKCBCentralManagerDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ internal protocol BKCBCentralManagerStateDelegate: class {
}

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

internal protocol BKCBCentralManagerConnectionDelegate: class {
func centralManager(_ central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral)
func centralManager(_ central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?)
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?)
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral)
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?)
func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?)
}

internal class BKCBCentralManagerDelegateProxy: NSObject, CBCentralManagerDelegate {
Expand All @@ -62,19 +62,24 @@ internal class BKCBCentralManagerDelegateProxy: NSObject, CBCentralManagerDelega
stateDelegate?.centralManagerDidUpdateState(central)
}

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

internal func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
connectionDelegate?.centralManager(central, didConnectPeripheral: peripheral)
connectionDelegate?.centralManager(central, didConnect: peripheral)
}

internal func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: NSError?) {
connectionDelegate?.centralManager(central, didFailToConnectPeripheral: peripheral, error: error)
internal func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
connectionDelegate?.centralManager(central, didFailToConnect: peripheral, error: error)
}

internal func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
internal func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
connectionDelegate?.centralManager(central, didDisconnectPeripheral: peripheral, error: error)
}





}
34 changes: 15 additions & 19 deletions Source/BKCBPeripheralManagerDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import CoreBluetooth

internal protocol BKCBPeripheralManagerDelegate: class {
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager)
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: NSError?)
func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?)
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?)
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?)
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeToCharacteristic characteristic: CBCharacteristic)
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFromCharacteristic characteristic: CBCharacteristic)
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWriteRequests requests: [CBATTRequest])
Expand All @@ -50,45 +50,41 @@ internal class BKCBPeripheralManagerDelegateProxy: NSObject, CBPeripheralManager
// MARK: CBPeripheralManagerDelegate

internal func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
// print("peripheralManagerDidUpdateState: \(peripheral)")
print("peripheralManagerDidUpdateState: \(peripheral)")
delegate?.peripheralManagerDidUpdateState(peripheral)
}

internal func peripheralManager(_ peripheral: CBPeripheralManager, willRestoreState dict: [String : AnyObject]) {
// print("peripheralManager: \(peripheral) willRestoreState: \(dict)")
}

internal func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: NSError?) {
// print("peripheralManagerDidStartAdvertising: \(peripheral) error: \(error)")

internal func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
print("peripheralManagerDidStartAdvertising: \(peripheral) error: \(error)")
delegate?.peripheralManagerDidStartAdvertising(peripheral, error: error)
}

internal func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: NSError?) {
// print("peripheralManager: \(peripheral) didAddService: \(service) error: \(error)")
delegate?.peripheralManager(peripheral, didAddService: service, error: error)
internal func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
print("peripheralManager: \(peripheral) didAddService: \(service) error: \(error)")
delegate?.peripheralManager(peripheral, didAdd: service, error: error)
}

internal func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
// print("peripheralManager: \(peripheral) central: \(central) didSubscribeToCharacteristic: \(characteristic)")
print("peripheralManager: \(peripheral) central: \(central) didSubscribeToCharacteristic: \(characteristic)")
delegate?.peripheralManager(peripheral, central: central, didSubscribeToCharacteristic: characteristic)
}

internal func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {
// print("peripheralManager: \(peripheral) central: \(central) didUnsubscribeFromCharacteristic: \(characteristic)")
print("peripheralManager: \(peripheral) central: \(central) didUnsubscribeFromCharacteristic: \(characteristic)")
delegate?.peripheralManager(peripheral, central: central, didUnsubscribeFromCharacteristic: characteristic)
}

internal func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {
// print("peripheralManager: \(peripheral) didReceiveReadRequest: \(request)")
print("peripheralManager: \(peripheral) didReceiveReadRequest: \(request)")
}

internal func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
// print("peripheralManager: \(peripheral) didReceiveWriteRequests: \(requests)")
print("peripheralManager: \(peripheral) didReceiveWriteRequests: \(requests)")
delegate?.peripheralManager(peripheral, didReceiveWriteRequests: requests)
}

internal func peripheralManagerIsReady(toUpdateSubscribers peripheral: CBPeripheralManager) {
// print("peripheralManagerIsReadyToUpdateSubscribers: \(peripheral)")
print("peripheralManagerIsReadyToUpdateSubscribers: \(peripheral)")
delegate?.peripheralManagerIsReadyToUpdateSubscribers(peripheral)
}

Expand Down
25 changes: 19 additions & 6 deletions Source/BKCentral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public protocol BKCentralDelegate: class {
- parameter central: The central from which it disconnected.
- parameter remotePeripheral: The remote peripheral that disconnected.
*/
@available(iOS 10.0, *)

func central(_ central: BKCentral, remotePeripheralDidDisconnect remotePeripheral: BKRemotePeripheral)
}

/**
The class used to take the Bluetooth LE central role. The central discovers remote peripherals by scanning
and connects to them. When a connection is established the central can receive data from the remote peripheral.
*/
@available(iOS 10.0, *)

public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoolDelegate, BKAvailabilityObservable {

// MARK: Type Aliases
Expand Down Expand Up @@ -71,10 +71,15 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
// MARK: Properties

/// Bluetooth LE availability, derived from the underlying CBCentralManager.
@available(iOS 10.0, *)

public var availability: BKAvailability? {
if let centralManager = _centralManager {
return BKAvailability(centralManagerState: centralManager.state)
if #available(iOS 10.0, *) {
return BKAvailability(centralState: centralManager.state)
}
else {
return BKAvailability(centralManagerState: CBCentralManagerState(rawValue: centralManager.state.rawValue)!)
}
} else {
return nil
}
Expand Down Expand Up @@ -183,7 +188,7 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
- parameter inBetweenDelay: The number of seconds to wait for, in-between scans (defaults to 3).
- parameter errorHandler: An error handler allowing you to react when an error occurs. For now this is also called when the scan is manually interrupted.
*/
@available(iOS 10.0, *)

public func scanContinuouslyWithChangeHandler(_ changeHandler: @escaping ContinuousScanChangeHandler, stateHandler: ContinuousScanStateHandler?, duration: TimeInterval = 3, inBetweenDelay: TimeInterval = 3, errorHandler: ContinuousScanErrorHandler?) {
do {
try stateMachine.handleEvent(.scan)
Expand Down Expand Up @@ -295,7 +300,15 @@ public class BKCentral: BKPeer, BKCBCentralManagerStateDelegate, BKConnectionPoo
case .unknown, .resetting:
break
case .unsupported, .unauthorized, .poweredOff:
let newCause = BKUnavailabilityCause(centralManagerState: central.state)
let newCause: BKUnavailabilityCause

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

switch stateMachine.state {
case let .unavailable(cause):
let oldCause = cause
Expand Down
9 changes: 5 additions & 4 deletions Source/BKConnectionPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,20 @@ internal class BKConnectionPool: BKCBCentralManagerConnectionDelegate {

// MARK: CentralManagerConnectionDelegate

internal func centralManager(_ central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) {
internal func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
succeedConnectionAttempt(connectionAttemptForPeripheral(peripheral)!)
}

internal func centralManager(_ central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) {
internal func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
failConnectionAttempt(connectionAttemptForPeripheral(peripheral)!, error: .internal(underlyingError: error))
}

internal func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) {
internal func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
if let remotePeripheral = connectedRemotePeripherals.filter({ $0.peripheral == peripheral }).last {
connectedRemotePeripherals.remove(at: connectedRemotePeripherals.index(of: remotePeripheral)!)
delegate?.connectionPool(self, remotePeripheralDidDisconnect: remotePeripheral)
}
}


}
4 changes: 2 additions & 2 deletions Source/BKDiscovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct BKDiscovery: Equatable {
}

/// The data advertised while the discovery was made.
public let advertisementData: [String: AnyObject]
public let advertisementData: [String: Any]

/// The remote peripheral that was discovered.
public let remotePeripheral: BKRemotePeripheral
Expand All @@ -51,7 +51,7 @@ public struct BKDiscovery: Equatable {

// MARK: Initialization

public init(advertisementData: [String: AnyObject], remotePeripheral: BKRemotePeripheral, RSSI: Int) {
public init(advertisementData: [String: Any], remotePeripheral: BKRemotePeripheral, RSSI: Int) {
self.advertisementData = advertisementData
self.remotePeripheral = remotePeripheral
self.RSSI = RSSI
Expand Down
31 changes: 24 additions & 7 deletions Source/BKPeripheral.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import CoreBluetooth
/**
The peripheral's delegate is called when asynchronous events occur.
*/
@available(iOS 10.0, *)

public protocol BKPeripheralDelegate: class {
/**
Called when a remote central connects and is ready to receive data.
Expand All @@ -48,16 +48,23 @@ public protocol BKPeripheralDelegate: class {
The class used to take the Bluetooth LE peripheral role. Peripherals can be discovered and connected to by centrals.
One a central has connected, the peripheral can send data to it.
*/
@available(iOS 10.0, *)

public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailabilityObservable {

// MARK: Properies

/// Bluetooth LE availability derived from the underlying CBPeripheralManager object.
@available(iOS 10.0, *)

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



/// The configuration that the BKPeripheral object was started with.
override public var configuration: BKPeripheralConfiguration? {
Expand Down Expand Up @@ -183,7 +190,16 @@ public class BKPeripheral: BKPeer, BKCBPeripheralManagerDelegate, BKAvailability
case .unknown, .resetting:
break
case .unsupported, .unauthorized, .poweredOff:
let newCause = BKUnavailabilityCause(peripheralManagerState: peripheral.state)

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


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

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

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

}

internal func peripheralManager(_ peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?) {
internal func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
if !peripheralManager.isAdvertising {
var advertisementData: [String: Any] = [ CBAdvertisementDataServiceUUIDsKey: _configuration.serviceUUIDs ]
if let localName = _configuration.localName {
Expand Down
2 changes: 1 addition & 1 deletion Source/BKScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ internal class BKScanner: BKCBCentralManagerDiscoveryDelegate {

// MARK: BKCBCentralManagerDiscoveryDelegate

internal func centralManager(_ central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String: AnyObject], RSSI: NSNumber) {
internal func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
guard busy else {
return
}
Expand Down

0 comments on commit cf860ba

Please sign in to comment.