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

当前执行时,我的日志如下:
蓝牙测试v1.00
正在打开蓝牙连接。。。
bean管理器初始化
寻找新的豆子。。。

最佳答案

我认为您的第一个问题是,您没有为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)
    }
}
}

关于ios - 在iOS上指定调度队列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26665832/

10-14 02:34