我正在为 OS X (10.10) 和 iOS 8 开发一个应用程序,以与 BLE 外围设备交换一些消息。我希望我的应用程序能够找到并连接到外围设备,向其发送消息,最后断开连接,以便可以从其他设备自由访问外围设备。除了以下特殊但仍然重要的情况外,一切正常:我扫描 BLE 设备并找到我的设备,但我仍然没有连接到它,因为我没有要发送的消息。然后我走开(超出范围)或者干脆关闭外围设备。我仍然将我的 CBPeripheral 保存在 var 中,并尝试连接到它。当我希望调用方法 centralManager:didFailToConnectPeripheral:error: 以便我可以再次开始扫描时,什么也没有发生。
centralManager:didConnectPeripheral 工作正常。该文档告诉蓝牙连接请求不会超时,所以我想知道还有哪些其他类型的问题可能导致连接失败
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();
}