首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >macOS CBCentralManager状态不受支持

macOS CBCentralManager状态不受支持
EN

Stack Overflow用户
提问于 2017-12-27 14:13:02
回答 1查看 2.5K关注 0票数 4

我正在尝试访问macOS (2017 iMac)上的蓝牙外设,但是CBCentralManager似乎从未进入.poweredOn状态。

代码语言:javascript
运行
复制
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,并显示控制台警告

代码语言:javascript
运行
复制
[CoreBluetooth] XPC connection invalid

我知道我尝试过的Info.plist key NSBluetoothPeripheralUsageDescription,尽管我相信它只适用于iOS设备。

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

EN

回答 1

Stack Overflow用户

发布于 2017-12-27 14:53:40

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

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

此外,禁用App Sandbox似乎是可行的,但是我的知识还不够丰富,不知道这样做是否安全。

作为参考,我的授权文件现在如下所示:

代码语言:javascript
运行
复制
<?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>    
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47987219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档