ios - 如何在 OSX 应用程序中使用核心蓝牙?

标签 ios objective-c macos swift core-bluetooth

我尝试在我目前正在开发的 OS X 应用程序中使用核心蓝牙框架,这实际上会将您的 Mac 连接到您的 iphone 以通过蓝牙 LE 执行不同的操作。

问题是,当我简单地初始化 CBCentralManager 和它的委托(delegate)方法时,我得到了一个奇怪的错误:

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

我不确定为什么会出现该错误,或​​者我做错了什么

最佳答案

我在 MacOS 10.13 上遇到了同样的错误,

  • 我首先将Privacy - Bluetooth Peripheral Usage Description 添加到 info.plist。
  • 接下来我发现在项目设置 -> 目标(你的应用程序) -> 功能 -> 应用程序沙盒 -> 蓝牙中必须检查。

Check Bluetooth

关于ios - 如何在 OSX 应用程序中使用核心蓝牙?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30427368/

相关文章:

objective-c - 行背景被切断

objective-c - (Mac) 面板大小调整问题

macos - sbt - Scala - Brew - MacOS - 根本无法连接到 sbt shell : NoClassDefFoundError

xcode - macOS 'wchar.h' 找不到文件

ios - AdMob GADBannerView 在 View 可见区域之外加载广告

ios - 如何在 ios 中将所有 uiview 重置为 nib 文件的原始帧值?

ios - 在 IBAction 中存储整数并将它们传递给另一个 IBAction

iphone - MKMapView上方的IOS activityIndi​​cator subview

iOS-indexPath 总是返回 nil

macos - 如何在终端中将 S-RET 发送到 Emacs?