【发布时间】:2018-06-07 19:20:25
【问题描述】:
我正在尝试访问 macOS (2017 iMac) 上的蓝牙外围设备,但 CBCentralManager 似乎从未进入 .poweredOn 状态。
import Cocoa
import CoreBluetooth
class BluetoothManager: NSObject {
var centralManager: CBCentralManager!
override init() {
super.init()
self.centralManager = CBCentralManager(delegate: self, queue:nil)
self.checkState()
}
func checkState() {
print("central state: \(self.centralManager?.state.rawValue ?? -1)")
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(2), execute: {
self.checkState()
})
}
}
extension BluetoothManager: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
print("Power on")
case .unsupported:
print("Unsupported")
default:break
}
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var bluetoothManager: BluetoothManager?
func applicationDidFinishLaunching(_ aNotification: Notification) {
self.bluetoothManager = BluetoothManager()
}
...
}
这将始终输出Unsupported,并带有控制台警告
[CoreBluetooth] XPC connection invalid
我知道Info.plist 键NSBluetoothPeripheralUsageDescription,我尝试过,但我相信这仅适用于iOS 设备。
我在 iMac 上管理蓝牙的方向是否错误?或者我的实现缺少什么?我觉得我已经涵盖了Core Bluetooth documentation 中所需的所有内容。
【问题讨论】:
标签: swift macos bluetooth core-bluetooth