2014-11-05 76 views
3

當我運行我的代碼並打開/關閉MacbookPro上的藍牙時,狀態始終爲4,對應於PoweredOff狀態。CBCentralManager狀態始終關閉

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") 
     } 
    } 
} 

我知道藍牙是事實上的,因爲我已經能夠將它與手機配對。

回答

3

回答我的問題...

事實證明,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.0測試在一個較新的MAC完全相同的代碼之後,狀態變成.PoweredOn

+0

如果你沒有得到你的機器上不支持的狀態,那很可能是一個錯誤。 – allprog 2014-11-06 22:20:36