macOS CBCentralManager状态不受支持

时间:2017-12-27 06:13:02

标签: swift macos bluetooth core-bluetooth

我正在尝试访问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中所需的所有内容。

1 个答案:

答案 0 :(得分:4)

我认为这是因为启用了SELECT terminal_name ,DATENAME(MONTH, sales_timestamp) AS Month_Name ,COUNT(nc_deposit) AS piece ,ISNULL( SUM(nc_deposit), 0 ) AS amount FROM ... (在项目App Sandbox中找到)。

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

同样禁用Hardware似乎也有效,但是我不知道这是否安全。

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

App Sandbox
相关问题