I have a file that implements the protocol and another file that calls the
//
// DeviceBLE.swift
//
import Foundation
import CoreBlueTooth
import QuartzCore
import UIKit
class DeviceBLE: NSObject, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager : CBCentralManager!
init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func centralManagerDidUpdateState(central: CBCentralManager!){
// Determine the state of the peripheral
if (central.state == .PoweredOff) {
println("CoreBluetooth BLE hardware is powered off")
}
else if (central.state == .PoweredOn) {
println("CoreBluetooth BLE hardware is powered on and ready")
// connectPeripheral(_ peripheral: CBPeripheral!,
// options options: [NSObject : AnyObject]!)
}
else if (central.state == .Unauthorized) {
println("CoreBluetooth BLE state is unauthorized")
}
else if (central.state == .Unknown) {
println("CoreBluetooth BLE state is unknown")
}
else if (central.state == .Unsupported) {
println("CoreBluetooth BLE hardware is unsupported on this platform")
}
}
}
this is the file calling
// ViewController.swift
// BleTest
import Foundation
import CoreBlueTooth
import QuartzCore
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var myBle = DeviceBLE()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
the problem is that centralManagerDidUpdateState never gets called when DeviceBle gets instantiated. I do not understand what I am doing wrong. Could anyone help?
A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements.
In iOS development, a protocol is a set of methods and properties that encapsulates a unit of functionality. The protocol doesn't actually contain any of the implementation for these things; it merely defines the required elements.
In Swift, a protocol defines a blueprint of methods or properties that can then be adopted by classes (or any other types). We use the protocol keyword to define a protocol. For example, protocol Greet { // blueprint of a property var name: String { get } // blueprint of a method func message() }
Callbacks are similar in function to the delegate pattern. They do the same thing: letting other objects know when something happened, and passing data around. What differentiates them from the delegate pattern, is that instead of passing a reference to yourself, you are passing a function.
It could be that you are setting myBle
to a local variable so by the end of that the viewDidLoad
method's execution, DeviceBLE is deallocated. Try making myBle an instance variable of the ViewController class.
class ViewController: UIViewController {
var myBle: CBCentralManager?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.myBle = DeviceBLE()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Also, in your delegate method. It'd be better practice to use a switch
statement instead of several if
statements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With