【问题标题】:macOS CBCentralManager state unsupportedmacOS CBCentralManager 状态不受支持
【发布时间】: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


    【解决方案1】:

    我相信这是由于启用了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>    
    

    【讨论】:

    • 这对我有帮助,谢谢!
    • 您还必须添加到您的 plist;示例;NSBluetoothAlwaysUsageDescription我们的应用使用蓝牙在不同设备之间查找、连接和传输数据
    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多