如何在iOS中列出可发现的蓝牙设备和已配对的设备,在swift中使用哪种方法? [英] How to List discoverable Bluetooth devices and already paired devices in iOS, which method to use in swift?

查看:492
本文介绍了如何在iOS中列出可发现的蓝牙设备和已配对的设备,在swift中使用哪种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to include Bluetooth feature on my app using swift. I want to list all nearby/discoverable peripheral devices including those devices are already paired

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

Which method i should use for listing the paired devices. Im using CoreBlutooth framework for implementing Bluetooth availability check. If Bluetooth works fine i would like to list out paired devices. And if possible please provide the method for connecting the device directly from the listed paired devices

 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)


}

//这个方法是不触发

//this method is not triggering

  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)


    }


}

这些方法是必要的,以显示附近的外围设备,如果不是要实施哪些方法和代码

is these methods are necessary to show the near by peripherals if not which methods and codes to be implemented

提前致谢

推荐答案

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

CoreBluetooth only allows you to access Bluetooth Low Energy devices. You can pair these devices if you need encryption but typically you don't.

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

However, there is no API to list the paired devices - you need to discover the device(s) that are offering the service you are interested in and present a list, if necessary using your own UI in your app. Once you have identified the target device you can initiate a connection.

这篇关于如何在iOS中列出可发现的蓝牙设备和已配对的设备,在swift中使用哪种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆