I'm developing an app for OS X (10.10) and iOS 8 to exchange some messages with a BLE peripheral. I want my app to find and connect to the peripheral, to send the message to it, and lastly to disconnect, so that the peripheral is free to be accessed from some other device. Everything works fine except for the following particular but still important situation: I scan for BLE devices and I find my device but still I don't connect to it because I have no messages to send. Then I walk away (out of range) or simply I turn the peripheral off. I still have saved in a var my CBPeripheral and I try to connect to it. Nothing happens while I would like the method centralManager:didFailToConnectPeripheral:error: to be called so that I can start to scan again.
centralManager:didConnectPeripheral working properly. The documentation tells that bluetooth connection requests don't time out so I was wondering what other kinds of problems could lead to a connection failure
func connect(){
if let peripheral = self.peripheralBLE { //Check if the CBPeripheral I was looking for is properly stored
centralManager!.connectPeripheral(peripheral, options: nil)
}
else {
println("Nothing to connect"); //Didn't find any peripheral
}
}
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
print("connected");
println(peripheral.name);
if (peripheral == nil) {
return;
}
// Create new service class
if (peripheral == self.peripheralBLE) { //if it is the peripheral I was looking for then initialize the service for communication
self.bleService = RFduino(initWithPeripheral: peripheral)
}
connectionStatus = .connected; //update connection status
// Stop scanning for new devices
central.stopScan()
}
func centralManager(central: CBCentralManager!, didFailToConnectPeripheral peripheral: CBPeripheral!, error: NSError!) {
println("Failed to connect peripheral");
if peripheral == self.peripheralBLE{
self.bleService = nil;
self.peripheralBLE = nil;
self.connectionStatus = .disconnected;
}
self.startScanning();
}