Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the protocol function not being called

Tags:

swift

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?

like image 488
Mehrdad Avatar asked Jul 14 '14 22:07

Mehrdad


People also ask

What is protocol and how do you implement it?

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.

What is a protocol in iOS?

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.

What does protocol mean in Swift?

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() }

What is the difference between delegate and callback in Swift?

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.


1 Answers

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.

like image 139
Alex Zielenski Avatar answered Nov 15 '22 07:11

Alex Zielenski