简体   繁体   English

BLE设备应无需扫描即可连接

[英]BLE Device Should get connected Without Scanning

BLE(Bluetooth Low Energy) Device Should get connected Without Scanning to my iPhone(App). BLE(蓝牙低能耗)设备应无需扫描即可连接到我的iPhone(App)。

I have the BLE Address which I'm getting through Scanning the QRCode. 我有通过扫描QRCode获得的BLE地址。

So from there I want the specific device(The one which I passed the address) connected without calling the (manager.scanForPeripherals(withServices: nil) ) As it will bring up all the devices . 所以从那里我想要连接特定的设备(我通过地址的设备)而不调用(manager.scanForPeripherals(withServices:nil)),因为它将启动所有设备。

For now I'm able to scan the QRCode of my BLE Device and Scan For All The Available devices then when I found my particular device. 现在,我能够扫描我的BLE设备的QRCode并扫描所有可用设备,然后找到我的特定设备。 I'm Stopping the scan and connecting to it by using advertisement Data in 我正在停止扫描并通过使用广告数据连接到它

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber)

Is there a way to connect directly without scanning as I already have the address of the BLE device 有没有办法直接连接而不进行扫描,因为我已经有了BLE设备的地址

Thanks in Advance .Any References would be helpful. 在此先感谢您。任何参考文献都会有所帮助。

You cannot connect to peripherals without performing a scan. 如果不执行扫描,则无法连接到外围设备。 While scanning the peripheral manager creates a unique id for each peripherals which is then used for connection. 在扫描时,外围设备管理器为每个外围设备创建一个唯一的ID,然后将其用于连接。 This unique id will be different for a same peripheral on different iPhones. 对于不同的iPhone,同一外围设备的唯一ID将有所不同。 So your idea will not work, you will have to perform a scan for connection. 因此,您的想法将行不通,您将必须执行连接扫描。

I don't know for Iphone but on windows it is. 我不知道Iphone,但在Windows上却是。 just connect with the known address. 只需连接已知地址即可。 It has some drawbacks: The device can be out of reach or already connected so you have to catch these situations. 它有一些缺点:设备可能无法触及或已经连接,因此您必须抓住这些情况。

If you wish to reconnect to a previously discovered device you can store the device UUID and than use: 如果您希望重新连接到以前发现的设备,则可以存储设备UUID,然后使用:

let retrievedPeripheral = centralManager.retrievePeripherals(withIdentifiers: [deviceUUID])

which will retrieve the CBPeripheral objects which match the provided UUID. 它将检索与提供的UUID匹配的CBPeripheral对象。 You can than perform the connection using: 然后,您可以使用以下命令执行连接:

centralManager.connect(retrievedPeripheral[0], options: nil)

This does mean that the device has to be in range and advertising and there really isn't a way to check for that unless you scan. 这确实意味着该设备必须在范围内并且可以做广告,并且除非您进行扫描,否则实际上没有任何方法可以检查它。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM