简体   繁体   English

在iOS应用中自动连接到BLE设备

[英]Auto connect to BLE device in iOS app

I am writing an iOS application which will communicate to a device using Bluetooth Low Energy (BLE). 我正在编写一个iOS应用程序,它将使用低功耗蓝牙(BLE)与设备进行通信。

When my connected device is out of range, my application is getting disconnect event. 当我连接的设备超出范围时,我的应用程序将收到断开连接事件。

But I am not getting any connect event when the device comes back to the range. 但是当设备返回范围时,我没有收到任何连接事件。

Please suggest any approach to detect when the device comes back to the range. 请提出任何检测设备何时返回范围的方法。

when you get the event that the device is no reachable anymore, you can launch a method which continuously checks if the device is still not reachable. 当您收到设备无法访问的事件时,可以启动一种方法来连续检查设备是否仍然无法访问。

For example: while(isConnected==false)... You say that you get a notification if the device disconnects, so you can use that event to launch such a method 例如:while(isConnected == false)...您说如果设备断开连接,您会收到一条通知,因此您可以使用该事件来启动这种方法。

When you receive the disconnect event, just restart the scan function: scanForPeripheralsWithServices or you can setup the scan mode to accept duplicate key by [_manager scanForPeripheralsWithServices:self.targetDeviceServiceIDs options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}] . 当您收到断开连接事件时,只需重新启动扫描功能: scanForPeripheralsWithServices或者可以通过[_manager scanForPeripheralsWithServices:self.targetDeviceServiceIDs options:@{CBCentralManagerScanOptionAllowDuplicatesKey:@YES}]设置扫描模式以接受重复密钥。 which means the same device would be discovered for many time until you stop scanning. 这意味着将在停止扫描之前多次发现同一设备。

Just reconnect in your disconnect method if the connection times out. 如果连接超时,只需使用断开连接方法重新连接。 There's no need to start a scan. 无需开始扫描。 It will automatically try to reconnect until it gets in range. 它将自动尝试重新连接,直到进入范围。

When your device get disconnected or goto out of range of Bluetooth then you don't need to scan for Peripheral devices because it already scanned for the BLE devices. 当您的设备断开连接或进入蓝牙范围之外时,您无需扫描外围设备,因为它已经扫描了BLE设备。

Call function [centralObj connectPeripheral:peripheral options:nil]; 调用函数[centralObj connectPeripheral:peripheral options:nil]; into didDisconnectPeripheral delegate method when BLE device disconnected. 当BLE设备断开连接时,进入didDisconnectPeripheral委托方法。

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

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