In the case where a peripheral has been discovered by a central, but right before user presses a button to connect, the peripheral goes out of range, so the connection cannot be completed. What method is called by CoreBluetooth? I thought that centralManager didFailToConnect would be called, but in my code it is not being called, perhaps because the connection never started at all? In this scenario which method should I use. Thanks in advance for any help.
public func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print("[ERROR] Could not connect to peripheral \(peripheral.identifier.uuidString) error: \(error!.localizedDescription)")
}
As said in the previous answer, none of the delegate methods will be called if any device does not initiated the connecting process.didFailToConnect
method only invoked if the device initiated a connection procedure but due to any reason that device could not able to connect.
If you want to check if any device is there for the connection, then you need to create one timer for the specific time period and if during that period if no device is scanned then you can show a message showing "no device available" and again check for devices.