Skip to content

Commit

Permalink
Merge pull request rhummelmose#54 from mrdavidrees/swift-3
Browse files Browse the repository at this point in the history
IMPORTANT: Libary will not work without these changes
  • Loading branch information
rhummelmose authored Oct 21, 2016
2 parents cd472bb + bd549a0 commit c0baddd
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 125 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)
}





}
79 changes: 34 additions & 45 deletions Source/BKCBPeripheralDelegateProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import CoreBluetooth

internal protocol BKCBPeripheralDelegate: class {
func peripheralDidUpdateName(_ peripheral: CBPeripheral)
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: NSError?)
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsForService service: CBService, error: NSError?)
func peripheral(_ peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?)
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?)
}

internal class BKCBPeripheralDelegateProxy: NSObject, CBPeripheralDelegate {
Expand All @@ -51,57 +51,46 @@ internal class BKCBPeripheralDelegateProxy: NSObject, CBPeripheralDelegate {
delegate?.peripheralDidUpdateName(peripheral)
}

internal func peripheral(_ peripheral: CBPeripheral, didModifyServices invalidatedServices: [CBService]) {
// print("peripheral: \(peripheral) didModifyServices invalidatedServices: \(invalidatedServices)")
}

internal func peripheralDidUpdateRSSI(_ peripheral: CBPeripheral, error: NSError?) {
// print("peripheralDidUpdateRSSI: \(peripheral), error: \(error)")
}

internal func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: NSError?) {
// print("peripheral: \(peripheral) didReadRSSI: \(RSSI), error: \(error)")
}

internal func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: NSError?) {
// print("peripheral: \(peripheral) didDiscoverServices error: \(error)")
internal func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
delegate?.peripheral(peripheral, didDiscoverServices: error)
}

internal func peripheral(_ peripheral: CBPeripheral, didDiscoverIncludedServicesFor service: CBService, error: NSError?) {
// print("peripheral: \(peripheral) didDiscoverIncludedServicesForService: \(service), error: \(error)")
internal func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
delegate?.peripheral(peripheral, didDiscoverCharacteristicsFor: service, error: error)
}

internal func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: NSError?) {
// print("peripheral: \(peripheral) didDiscoverCharacteristicsForService: \(service), error: \(error)")
delegate?.peripheral(peripheral, didDiscoverCharacteristicsForService: service, error: error)

internal func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
delegate?.peripheral(peripheral, didUpdateValueFor: characteristic, error: error)
}

internal func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: NSError?) {
// print("peripheral: \(peripheral) didUpdateValueForCharacteristic: \(characteristic), error: \(error)")
delegate?.peripheral(peripheral, didUpdateValueForCharacteristic: characteristic, error: error)

internal func peripheral(_ peripheral: CBPeripheral, didReadRSSI RSSI: NSNumber, error: Error?) {

}

internal func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: NSError?) {
// print("peripheral: \(peripheral), didWriteValueForCharacteristic: \(characteristic), error: \(error)")

@nonobjc
internal func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {

}

internal func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, error: NSError?) {
// print("peripheral: \(peripheral) didUpdateNotificationStateForCharacteristic: \(characteristic), error: \(error)")

@nonobjc
internal func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?) {

}

internal func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: NSError?) {
// print("peripheral: \(peripheral) didDiscoverDescriptorsForCharacteristic: \(characteristic), error: \(error)")
internal func peripheral(_ peripheral: CBPeripheral, didDiscoverIncludedServicesFor service: CBService, error: Error?) {

}

@nonobjc
internal func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: NSError?) {
// print("peripheral: \(peripheral) didUpdateValueForDescriptor: \(descriptor), error: \(error)")

internal func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {

}

@nonobjc
internal func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: NSError?) {
// print("peripheral: \(peripheral) didWriteValueForDescriptor: \(descriptor), error: \(error)")

internal func peripheral(_ peripheral: CBPeripheral, didDiscoverDescriptorsFor characteristic: CBCharacteristic, error: Error?) {

}

internal func peripheral(_ peripheral: CBPeripheral, didUpdateNotificationStateFor characteristic: CBCharacteristic, 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
Loading

0 comments on commit c0baddd

Please sign in to comment.