我碰到了同樣的問題,同時建立一個非常基本的BLE掃描儀應用中移除調用scanForPeripheralsWithServices
。添加了所需的方法「centralManagerDidUpdateState」。但沒有任何工作。
我相信問題與queue
有關。 把CBCentralManager
例如在dispatch_get_main_queue
這段代碼確實是:
// BLE Stuff
let myCentralManager = CBCentralManager()
// Put CentralManager in the main queue
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
myCentralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())
}
使用默認的單一視圖Xcode中開始應用。你可以把這個到ViewController.swift文件:
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
// BLE Stuff
let myCentralManager = CBCentralManager()
var peripheralArray = [CBPeripheral]() // create now empty array.
// Put CentralManager in the main queue
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
myCentralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// Mark CBCentralManager Methods
func centralManagerDidUpdateState(central: CBCentralManager!) {
updateStatusLabel("centralManagerDidUpdateState")
switch central.state{
case .PoweredOn:
updateStatusLabel("poweredOn")
case .PoweredOff:
updateStatusLabel("Central State PoweredOFF")
case .Resetting:
updateStatusLabel("Central State Resetting")
case .Unauthorized:
updateStatusLabel("Central State Unauthorized")
case .Unknown:
updateStatusLabel("Central State Unknown")
case .Unsupported:
println("Central State Unsupported")
default:
println("Central State None Of The Above")
}
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
println("Did Discover Peripheral")
}
}
[CBCentralManager的狀態何時會啓動但仍然給我一個「未通電」的警告?](http://stackoverflow.com/questions/17118534/when-would-cbcentralmanagers-state-ever-被啓動,但仍然給我不) – 2014-09-01 19:58:35