在iOS上指定调度队列

时间:2014-10-31 00:44:06

标签: ios inheritance swift

使用 Swift ,我有一个自定义类包装CBCentralManager类,如下所示:

import UIKit
import CoreBluetooth

protocol vaBeanDelegate
{

}

class vaBean: CBCentralManager, CBCentralManagerDelegate {

override init() {
    println("bean manager initialised")
    super.init()
    self.delegate = self
}

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
    println("discovered peripheral(s)")
}

func centralManagerDidUpdateState(central: CBCentralManager!) {
    switch (central.state) {
    case .PoweredOff:
        println("hardware is powered off")

    case .PoweredOn:
        println("hardware is powered on and ready")
        //centralManager.scanForPeripheralsWithServices(nil, options: nil)
    case .Resetting:
        println("hardware is resetting")

    case .Unauthorized:
        println("state is unauthorized")

    case .Unknown:
        println("state is unknown");

    case .Unsupported:
        println("hardware is unsupported on this platform");

    }
}

func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
    println("didConnectPeripheral")
}

func searchForNewBeans() {
    println("searching for new beans...")
    self.scanForPeripheralsWithServices(nil, options: nil)
}
}

课程初始化,然后我可以调用searchForNewBeans函数,但我没有收到来自didUpdateStatedidDiscoverPeripheral函数的任何回复。我怀疑它是因为我的新班级不知道它应该是这些的接收器,但我如何以及在何处指定?我的ViewController中的调用代码如下:

import UIKit

class ViewController: UIViewController {

var myBean: vaBean!

override func viewDidLoad() {
    super.viewDidLoad()
    println("blueToothTest v1.00")
    println("opening bluetooth connection ...")
    myBean = vaBean()
    myBean.searchForNewBeans()
}

override func didReceiveMemoryWarning() {super.didReceiveMemoryWarning()}
}

我当前执行时的日志如下:

blueToothTest v1.00

打开蓝牙连接...

bean manager初始化

寻找新豆......

1 个答案:

答案 0 :(得分:2)

我认为您的第一个问题是您没有为CBCentralManager调用指定的初始化程序。

此外,在scanForPeripheralsWithServices状态之前,您无法致电PoweredOn

编写当前代码的方式无疑会在此状态之前调用scanForPeripheralsWithServices

我建议如下:

import UIKit
import CoreBluetooth

protocol vaBeanDelegate
{

}

class vaBean: CBCentralManager, CBCentralManagerDelegate {

var isScanning=false
var isPowered=false

 override init() {

    println("bean manager initialised")
    super.init(delegate: nil, queue: nil, options:[NSObject : AnyObject]())   //Initialise with self as the delegate and default queue
    self.delegate=self

}

func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
    println("discovered peripheral(s)")
}

func centralManagerDidUpdateState(central: CBCentralManager!) {
    switch (central.state) {
    case .PoweredOff:
        println("hardware is powered off")

    case .PoweredOn:
        println("hardware is powered on and ready")
        self.isPowered=true
        if (self.isScanning) {
           self.searchForNewBeans()
        }
        //centralManager.scanForPeripheralsWithServices(nil, options: nil)
    case .Resetting:
        println("hardware is resetting")

    case .Unauthorized:
        println("state is unauthorized")

    case .Unknown:
        println("state is unknown");

    case .Unsupported:
        println("hardware is unsupported on this platform");

    }
}

func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
    println("didConnectPeripheral")
}

func searchForNewBeans() {
    self.isScanning=true;
    if (self.isPowered) {
        println("searching for new beans...")
        self.scanForPeripheralsWithServices(nil, options: nil)
    }
}
}