swift - 如何在 iOS 中列出可发现的蓝牙设备和已配对的设备,快速使用哪种方法?

标签 swift ios8 xcode6 core-bluetooth

我想使用 swift 在我的应用程序中包含蓝牙功能。我想列出所有附近/可发现的外围设备,包括那些已经配对的设备

我应该使用哪种方法来列出配对设备。我正在使用 CoreBlutooth 框架来实现蓝牙可用性检查。如果蓝牙工作正常,我想列出配对的设备。如果可能,请提供直接从列出的配对设备连接设备的方法

 func startUpCentralManager() {
    println("Initializing central manager")
    centralManager = CBCentralManager(delegate: self, queue: nil)


}



func centralManagerDidUpdateState(central: CBCentralManager!) {
    println("\(__LINE__) \(__FUNCTION__)")
     println("checking state")

    if central.state != .PoweredOn {
        // In a real app, you'd deal with all the states correctly
                    return
    }
    central.scanForPeripheralsWithServices(nil,options:nil)


}

//此方法不触发

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

    var localname: NSString = advertisementData[CBAdvertisementDataLocalNameKey]! as NSString

    println("Discovered\(peripheral.name)")

    if localname != " "
    {
        centralManager!.stopScan()
        //self.peripheral = peripheral
        peripheral.delegate = self
        centralManager!.connectPeripheral(peripheral, options: nil)


    }


}

是否需要这些方法来显示附近的外围设备,如果不是,则要实现哪些方法和代码

提前致谢

最佳答案

CoreBluetooth 只允许您访问低功耗蓝牙设备。如果您需要加密,您可以将这些设备配对,但通常不需要。

但是,没有用于列出配对设备的 API - 您需要发现提供您感兴趣的服务的设备并提供一个列表,如有必要,请在您的应用中使用您自己的 UI。确定目标设备后,您可以启动连接。

关于swift - 如何在 iOS 中列出可发现的蓝牙设备和已配对的设备,快速使用哪种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28337159/

相关文章:

ios - 如何以正确的方式使用导航 Controller 实现标签栏 Controller

ios - 如何在后台收到通知时显示警报和播放声音

xcode5 - ios 8,如何使用ALAssetLibrary获取所有照片来替换丢失的相机胶卷相册

objective-c - 有没有办法用 Swift 查看我的物理体的大小?

iOS 8 扩展依赖问题。将一个项目文件导入扩展 View Controller

ios - UIImageView 无法在自调整单元格 iOS 8 中正确调整大小

ios - fatal error : unexpectedly found nil while unwrapping an Optional value - Swift with NSURLRequest

ios - 以编程方式启用按钮需要几秒钟

ios - SKAction.animate 与 SKTextureAtlas 方法不动画

python - 将 Django json 导入 iPhone App