user4860206
user4860206

Reputation: 51

How to use Core Bluetooth in an OSX app?

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

Answers (2)

Lennart Lenin Olsen
Lennart Lenin Olsen

Reputation: 96

I had the same Error with MacOS 10.13,

  • I started by adding the Privacy - Bluetooth Peripheral Usage Description to the info.plist.
  • Next i found that in Project Settings -> Target (your app) -> Capabilities -> App Sandbox -> Bluetooth had to be checked.

Check Bluetooth

Upvotes: 6

Tobias
Tobias

Reputation: 4397

I can think of two causes.

  1. You haven't added CoreBluetooth as a linked framework.

  2. 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

Related Questions