问题描述
我一般都不熟悉Swift和Mac/iOs编程.我正在使用Xcode 7.3在具有BLE支持并已打开蓝牙的Macbook Pro上运行此示例.
I'm new to Swift and Mac/iOs programming in general. I'm running this sample on a Macbook Pro with BLE support and with Bluetooth turned on, using Xcode 7.3.
import Foundation
import CoreBluetooth
func printState(state: CBCentralManagerState) {
switch state {
case CBCentralManagerState.PoweredOn:
print("Powered on")
case CBCentralManagerState.PoweredOff:
print("Powered off")
case CBCentralManagerState.Resetting:
print("Resetting")
case CBCentralManagerState.Unauthorized:
print("Unauthorized")
case CBCentralManagerState.Unknown:
print("Unknown")
default:
print ("Unsupported")
}
}
var myCentralManager = CBCentralManager(delegate:nil, queue:nil)
while true {
printState(myCentralManager.state)
sleep(1)
}
即使在几分钟后,该代码也会反复打印出未知"字样.我也尝试过设置一个委托,但是didUpdateState回调没有被调用.我还尝试从命令行和Swift解释器运行此命令,并获得相同的结果.
The code prints out "Unknown" over and over, even after many minutes. I've also tried setting up a delegate but the didUpdateState callback doesn't called. I've also tried to run this from the command line and Swift interpreter and get the same result.
我一定错过了一些非常基本的东西.如何获得报告CBCentralManager已打开电源的信息?
I must be missing something very basic. How can I get it to report that the CBCentralManager is powered on?
推荐答案
据我所知,如果您不使用文档中列出的两个初始值设定项之一,那么结果就是-您想使用其中一个:
Near as I can tell, if you don't use one of the two initializers listed in the documentation, this is the result -- you want to use either:
init(delegate:queue:)
或:
init(delegate:queue:options:)
如果您致电,Xcode不会抱怨:
Xcode doesn't complain if you just call:
CBCentralManager()
但是,如果您这样做,似乎您将无限期地停留在CBCentralManagerState.Unknown
中.
but if you do that, it seems like you'll stay in CBCentralManagerState.Unknown
indefinitely.
如果要进行实验,请创建一个操场并粘贴此代码,然后尝试使用我列出的两个初始化变量:
If you want to experiment, make a playground and paste this code, and try it with the two initializer variants I've listed:
import Cocoa
import CoreBluetooth
import XCPlayground
@objc
public class BluetoothWatcher: NSObject, CBCentralManagerDelegate {
var cbcm: CBCentralManager!;
var timer: NSTimer!;
override init() {
super.init();
/*:
When you initialize a central manager, the delegate seems important. If you comment out the initializer with the delegate and uncomment the other one, it'll stay in state unknown indefinitely.
*/
//cbcm = CBCentralManager(delegate: self, queue:nil);
cbcm = CBCentralManager();
checkStateInOneSecond();
}
public func centralManagerDidUpdateState(central: CBCentralManager) {
print( "State updated: \(stateString())" );
}
func checkStateInOneSecond() {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), {
self.checkState();
});
}
public func checkState() {
print( "Timer fired, state: \(stateString())" );
checkStateInOneSecond();
}
func stateString() -> String {
switch(cbcm.state) {
case .Resetting:
return "resetting"
case .PoweredOn:
return "powered on";
case .PoweredOff:
return "powered off";
case .Unknown:
return "unknown";
case .Unsupported:
return "unsupported";
case .Unauthorized:
return "unauthorized";
}
}
}
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true;
var watcher = BluetoothWatcher();
您将看到与委托有关的内容,它从Unknown
开始,并且几乎立即以状态更改为PoweredOn
的方式调用了该委托,并且随后的每个计时器触发都显示PoweredOn
.
You'll see with the delegate, it starts in Unknown
, and almost immediately the delegate gets called with a state change to PoweredOn
, and every subsequent timer fire shows PoweredOn
.
在没有委托的情况下使用空的初始化程序执行相同的操作,并且永远不会调用委托方法(毫不奇怪),并且计时器触发将继续显示Unknown
.
Do the same thing without the delegate, with the empty initializer, and the delegate method will never be called (no surprise) and the timer fires will continue to show Unknown
.
这篇关于核心蓝牙CBCentralManager在Mac上始终将状态报告为“未知"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!