【问题标题】:CBCentralManager state is always offCBCentralManager 状态总是关闭
【发布时间】:2014-11-05 18:11:14
【问题描述】:

当我运行我的代码并在我的 MacbookPro 上打开/关闭蓝牙时,状态始终为 4,对应于 PoweredOff 状态。

import Cocoa
import CoreBluetooth

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, CBCentralManagerDelegate {

    var centralManager = CBCentralManager()

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        centralManager = CBCentralManager(delegate: self, queue: nil)
    }

    func centralManagerDidUpdateState(central: CBCentralManager!) {

        switch central.state {

            case .PoweredOn:
                println(".PoweredOn")

            case .PoweredOff:
                println(".PoweredOff")

            case .Resetting:
                println(".Resetting")

            case .Unauthorized:
                println(".Unauthorized")

            case .Unknown:
                println(".Unknown")

            case .Unsupported:
                println(".Unsupported")
        }
    }
}

我知道蓝牙实际上已开启,因为我已经能够将它与我的手机配对。

【问题讨论】:

    标签: swift osx-mavericks core-bluetooth


    【解决方案1】:

    回答我自己的问题...

    原来CoreBluetooth只适用于蓝牙4.0:

    核心蓝牙框架是蓝牙 4.0 规范(source)的抽象

    要了解您的 Mac 的蓝牙规格:

     > About This Mac > More Info... > System Report... > Hardware > Bluetooth

    寻找LMP Version

    0x4 = Bluetooth Core Specification 2.1 + EDR
    0x6 = Bluetooth Core Specification 4.0
    

    我有 LMP 版本 4,所以我猜CoreBluetooth 对我不起作用。

    有趣的是,switch 语句并没有给我.Unsupported 的情况。


    编辑:
    在具有蓝牙 4 的新 Mac 上测试完全相同的代码后,状态变为 .PoweredOn

    【讨论】:

    • 如果您的机器上没有得到不受支持的状态,那么这很可能是一个错误。
    猜你喜欢
    • 2018-05-14
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多