Reputation: 51
I tried using the Core Bluetooth framework in an OS X app i am currently developing, which would essentially connect your Mac to your iphone to do different things over Bluetooth LE.
Problem is, when I simply initialized a CBCentralManager and it's delegate methods I get a weird error:
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)
This is my code below:
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
}
}
I am not sure why I am getting that error, or what I am doing wrong
Upvotes: 1
Views: 4736
Reputation: 96
I had the same Error with MacOS 10.13,
Upvotes: 6
Reputation: 4397
I can think of two causes.
You haven't added CoreBluetooth as a linked framework.
You are attempting to run your code in a simulator. The simulator however does not have a Bluetooth device it can use. So you'll have to run all Bluetooth code on a device. I have run across some tutorials about how to use a Bluetooth dongle and get it to work with the simulator, but I've never succeeded.
Also I noticed you haven't set the delegate of the CBCentralManager.
self.centralManager = CBCentralManager(delegate: nil, queue: nil)
Shouldn't that line be,
self.centralManager = CBCentralManager(delegate: self, queue: nil)
Upvotes: 3