Why is the protocol function not being called

swift

Solution

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.

Problem

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?

Original source