我正在尝试访问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
key NSBluetoothPeripheralUsageDescription
,尽管我相信它只适用于iOS设备。
我在iMac上管理蓝牙的方向错了吗?还是我的实现遗漏了什么?我觉得我已经涵盖了Core Bluetooth documentation要求的所有内容。
发布于 2017-12-27 14:53:40
我相信这是由于启用了App Sandbox
(在项目Capabilities
中找到)。
启用Bluetooth
(在Hardware
下)并接受对授权文件的自动更改解决了问题。
此外,禁用App Sandbox
似乎是可行的,但是我的知识还不够丰富,不知道这样做是否安全。
作为参考,我的授权文件现在如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.device.bluetooth</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
https://stackoverflow.com/questions/47987219
复制相似问题