Skip to content

Updated to Swift 4 syntax #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 31 additions & 31 deletions BLEFramework/BLE/BLE.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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)"
Expand All @@ -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()
}
Expand All @@ -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)
}
}

Expand All @@ -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?) {
Expand All @@ -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)
}
}

Expand Down