2015-05-24 33 views
0

我嘗試在我正在開發的OS X應用程序中使用Core藍牙框架,它基本上將Mac連接到您的iPhone,以通過藍牙LE執行不同的操作。如何在OSX應用程序中使用Core藍牙?

問題是,當我只是初始化CBCentralManager和它的委託方法我得到一個奇怪的錯誤:

Undefined symbols for architecture x86_64: 
    "_CBAdvertisementDataLocalNameKey", referenced from: 
     __TFC5test311AppDelegate14centralManagerfS0_FTGSQCSo16CBCentralManager_21didDiscoverPeripheralGSQCSo12CBPeripheral_17advertisementDataGSQGVSs10DictionaryCSo8NSObjectPSs9AnyObject___4RSSIGSQCSo8NSNumber__T_ in AppDelegate.o 
    "_OBJC_CLASS_$_CBCentralManager", referenced from: 
     __TMaCSo16CBCentralManager in AppDelegate.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

這是我下面的代碼:

import Cocoa 
import CoreBluetooth 

@NSApplicationMain 
class AppDelegate: NSObject, NSApplicationDelegate, CBCentralManagerDelegate { 

    @IBOutlet weak var window: NSWindow! 
    var centralManager: CBCentralManager! 

    func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) { 

     var localName: String = advertisementData[CBAdvertisementDataLocalNameKey] as String 

     if (countElements(localName) > 0) { 
      println("Found Mac: \(localName)") 
      self.centralManager.stopScan() 
     } else { 
      println("Not Found") 
     } 
    } 
    func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) { 
     println("1") 
    } 

    func centralManager(central: CBCentralManager!, didDisconnectPeripheral peripheral: CBPeripheral!, error: NSError!) { 
     println("2") 
    } 

    func centralManagerDidUpdateState(central: CBCentralManager!) { 
     switch (central.state) { 
     case .PoweredOff: 
      println("Powered Off") 
     case .PoweredOn: 
      println("Powered On") 
      self.centralManager.scanForPeripheralsWithServices(nil, options: nil) 
     case .Unauthorized: 
      println("Unauthorized") 
     case .Unknown: 
      println("Unknown") 
     case .Unsupported: 
      println("Unsupported") 
     default: 
      println("Default") 
     } 
    } 

    func applicationDidFinishLaunching(aNotification: NSNotification) { 
     self.centralManager = CBCentralManager(delegate: nil, queue: nil) 
     // Insert code here to initialize your application 
    } 

    func applicationWillTerminate(aNotification: NSNotification) { 
     // Insert code here to tear down your application 
    } 
} 

我不知道爲什麼我得到那個錯誤,或者我做錯了什麼

回答

2

我能想到兩個原因。

  1. 您還沒有添加CoreBluetooth作爲鏈接框架。

  2. 您正試圖在模擬器中運行您的代碼。然而,模擬器沒有可以使用的藍牙設備。所以你必須在設備上運行所有的藍牙代碼。我遇到了一些關於如何使用藍牙適配器並使其與模擬器一起工作的教程,但我從未成功過。

另外我注意到你還沒有設置CBCentralManager的委託。

self.centralManager = CBCentralManager(delegate: nil, queue: nil) 

難道不應該行,

self.centralManager = CBCentralManager(delegate: self, queue: nil) 
+0

哈哈,是的我忘了將其添加爲鏈接的框架,感謝的人 – user4860206

+0

很高興我能幫忙。我讓那些「doh!」不斷有錯誤。 – Tobias

+0

與我創建的CocoaPods庫有相同的問題。需要將'spec.framework ='CoreBluetooth''添加到podspec文件 –

0

我有同樣的錯誤和MacOS 10.13,

  • 我開始通過增加隱私 - 藍牙外設使用說明到info.plist。
  • 接下來,我發現在項目設置 - >目標(你的應用程序) - >功能 - >應用程序沙箱 - >藍牙必須檢查。

Check Bluetooth