diff --git a/BLEFramework/BLE/BLE.swift b/BLEFramework/BLE/BLE.swift index 0ab1297..9af2291 100644 --- a/BLEFramework/BLE/BLE.swift +++ b/BLEFramework/BLE/BLE.swift @@ -51,7 +51,7 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { // MARK: Public methods func startScanning(timeout: Double) -> Bool { - if self.centralManager.state != .PoweredOn { + if self.centralManager.state != .poweredOn { print("[ERROR] Couldn´t start scanning") return false @@ -61,32 +61,32 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { // CBCentralManagerScanOptionAllowDuplicatesKey - NSTimer.scheduledTimerWithTimeInterval(timeout, target: self, selector: #selector(BLE.scanTimeout), userInfo: nil, repeats: false) + Timer.scheduledTimer(timeInterval: timeout, target: self, selector: #selector(BLE.scanTimeout), userInfo: nil, repeats: false) let services:[CBUUID] = [CBUUID(string: RBL_SERVICE_UUID)] - self.centralManager.scanForPeripheralsWithServices(services, options: nil) + self.centralManager.scanForPeripherals(withServices: services, options: nil) return true } func connectToPeripheral(peripheral: CBPeripheral) -> Bool { - if self.centralManager.state != .PoweredOn { + if self.centralManager.state != .poweredOn { print("[ERROR] Couldn´t connect to peripheral") return false } - print("[DEBUG] Connecting to peripheral: \(peripheral.identifier.UUIDString)") + print("[DEBUG] Connecting to peripheral: \(peripheral.identifier.uuidString)") - self.centralManager.connectPeripheral(peripheral, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : NSNumber(bool: true)]) + self.centralManager.connect(peripheral, options: [CBConnectPeripheralOptionNotifyOnDisconnectionKey : NSNumber(value: true)]) return true } func disconnectFromPeripheral(peripheral: CBPeripheral) -> Bool { - if self.centralManager.state != .PoweredOn { + if self.centralManager.state != .poweredOn { print("[ERROR] Couldn´t disconnect from peripheral") return false @@ -101,54 +101,54 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { guard let char = self.characteristics[RBL_CHAR_TX_UUID] else { return } - self.activePeripheral?.readValueForCharacteristic(char) + self.activePeripheral?.readValue(for: char) } func write(data data: NSData) { guard let char = self.characteristics[RBL_CHAR_RX_UUID] else { return } - self.activePeripheral?.writeValue(data, forCharacteristic: char, type: .WithoutResponse) + self.activePeripheral?.writeValue(data as Data, for: char, type: .withoutResponse) } func enableNotifications(enable: Bool) { guard let char = self.characteristics[RBL_CHAR_TX_UUID] else { return } - self.activePeripheral?.setNotifyValue(enable, forCharacteristic: char) + self.activePeripheral?.setNotifyValue(enable, for: char) } - func readRSSI(completion: (RSSI: NSNumber?, error: NSError?) -> ()) { + func readRSSI(completion: @escaping (_ RSSI: NSNumber?, _ error: NSError?) -> ()) { self.RSSICompletionHandler = completion self.activePeripheral?.readRSSI() } // MARK: CBCentralManager delegate - func centralManagerDidUpdateState(central: CBCentralManager) { + func centralManagerDidUpdateState(_ central: CBCentralManager) { switch central.state { - case .Unknown: + case .unknown: print("[DEBUG] Central manager state: Unknown") break - case .Resetting: + case .resetting: print("[DEBUG] Central manager state: Resseting") break - case .Unsupported: + case .unsupported: print("[DEBUG] Central manager state: Unsopported") break - case .Unauthorized: + case .unauthorized: print("[DEBUG] Central manager state: Unauthorized") break - case .PoweredOff: + case .poweredOff: print("[DEBUG] Central manager state: Powered off") break - case .PoweredOn: + case .poweredOn: print("[DEBUG] Central manager state: Powered on") break } @@ -157,9 +157,9 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { } func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject],RSSI: NSNumber) { - print("[DEBUG] Find peripheral: \(peripheral.identifier.UUIDString) RSSI: \(RSSI)") + print("[DEBUG] Find peripheral: \(peripheral.identifier.uuidString) RSSI: \(RSSI)") - let index = peripherals.indexOf { $0.identifier.UUIDString == peripheral.identifier.UUIDString } + let index = peripherals.index { $0.identifier.uuidString == peripheral.identifier.uuidString } if let index = index { peripherals[index] = peripheral @@ -169,12 +169,12 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { } func centralManager(central: CBCentralManager, didFailToConnectPeripheral peripheral: CBPeripheral, error: NSError?) { - print("[ERROR] Could not connecto to peripheral \(peripheral.identifier.UUIDString) error: \(error!.description)") + print("[ERROR] Could not connecto to peripheral \(peripheral.identifier.uuidString) error: \(error!.description)") } func centralManager(central: CBCentralManager, didConnectPeripheral peripheral: CBPeripheral) { - print("[DEBUG] Connected to peripheral \(peripheral.identifier.UUIDString)") + print("[DEBUG] Connected to peripheral \(peripheral.identifier.uuidString)") self.activePeripheral = peripheral @@ -186,7 +186,7 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { func centralManager(central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: NSError?) { - var text = "[DEBUG] Disconnected from peripheral: \(peripheral.identifier.UUIDString)" + var text = "[DEBUG] Disconnected from peripheral: \(peripheral.identifier.uuidString)" if error != nil { text += ". Error: \(error!.description)" @@ -196,7 +196,7 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { self.activePeripheral?.delegate = nil self.activePeripheral = nil - self.characteristics.removeAll(keepCapacity: false) + self.characteristics.removeAll(keepingCapacity: false) self.delegate?.bleDidDisconenctFromPeripheral() } @@ -209,13 +209,13 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { return } - print("[DEBUG] Found services for peripheral: \(peripheral.identifier.UUIDString)") + print("[DEBUG] Found services for peripheral: \(peripheral.identifier.uuidString)") for service in peripheral.services! { let theCharacteristics = [CBUUID(string: RBL_CHAR_RX_UUID), CBUUID(string: RBL_CHAR_TX_UUID)] - peripheral.discoverCharacteristics(theCharacteristics, forService: service) + peripheral.discoverCharacteristics(theCharacteristics, for: service) } } @@ -226,13 +226,13 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { return } - print("[DEBUG] Found characteristics for peripheral: \(peripheral.identifier.UUIDString)") + print("[DEBUG] Found characteristics for peripheral: \(peripheral.identifier.uuidString)") for characteristic in service.characteristics! { - self.characteristics[characteristic.UUID.UUIDString] = characteristic + self.characteristics[characteristic.uuid.uuidString] = characteristic } - enableNotifications(true) + enableNotifications(enable: true) } func peripheral(peripheral: CBPeripheral, didUpdateValueForCharacteristic characteristic: CBCharacteristic, error: NSError?) { @@ -243,9 +243,9 @@ class BLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate { return } - if characteristic.UUID.UUIDString == RBL_CHAR_TX_UUID { + if characteristic.uuid.uuidString == RBL_CHAR_TX_UUID { - self.delegate?.bleDidReceiveData(characteristic.value) + self.delegate?.bleDidReceiveData(data: characteristic.value as! NSData) } }