我正在尝试收听所有蓝牙连接/断开事件.即使centralManagerDidUpdateState调用了委托的功能,当我连接或断开蓝牙设备时也没有任何反应.
我对于出了什么问题很困惑.我像这样初始化Central Manager/delegate:
var btDelegate: CBCentralManagerDelegate = BluetoothDelegate()
var btManager = CBCentralManager(delegate: btDelegate, queue: nil)
Run Code Online (Sandbox Code Playgroud)
BluetoothDelegate的定义如下:
import Foundation
import CoreBluetooth
class BluetoothDelegate : NSObject, CBCentralManagerDelegate {
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
println("connect") //this line is not called
}
func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
println("disconnect") //this line is not called
}
func centralManagerDidUpdateState(central: CBCentralManager!) {
println("state update") //this line is called
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我可以不断收到更多状态更新事件,例如我切换蓝牙时,即使没有调用连接和断开连接.