Auto connect to BLE device in iOS app

2019-07-13 02:49发布

问题:

I am writing an iOS application which will communicate to a device using Bluetooth Low Energy (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.

回答1:

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



回答2:

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}]. which means the same device would be discovered for many time until you stop scanning.



回答3:

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.



回答4:

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.

Call function [centralObj connectPeripheral:peripheral options:nil]; into didDisconnectPeripheral delegate method when BLE device disconnected.