gpt4 book ai didi

swift - macOS CBCentralManager 状态不受支持

转载 作者:行者123 更新时间:2023-11-28 09:51:03 29 4
gpt4 key购买 nike

我正在尝试访问 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.plistNSBluetoothPeripheralUsageDescription,我试过了,但我相信它只适用于 iOS 设备。

在 iMac 上管理蓝牙的方向是否错误?还是我的实现遗漏了什么?我觉得我已经涵盖了 Core Bluetooth documentation 中要求的所有内容。 .

最佳答案

我相信这是由于启用了 App Sandbox(在项目 Capabilities 中找到)。

启用蓝牙(在硬件下),并接受对授权文件的自动更改解决了问题。

同时禁用 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>

关于swift - macOS CBCentralManager 状态不受支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47987219/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com