CBCentralManager didFailToConnectPeripheral

时间:2015-08-05 20:18:36

标签: ios iphone macos bluetooth cbperipheral

我正在为OS X(10.10)和iOS 8开发一个应用程序来与BLE外围设备交换一些消息。我希望我的应用程序找到并连接到外围设备,将消息发送给它,最后断开连接,以便可以从其他设备免费访问外围设备。一切正常,除了以下特殊但仍然很重要的情况:我扫描BLE设备,我找到了我的设备,但我仍然没有连接到它,因为我没有要发送的消息。然后我走开(超出范围)或只是我关闭周边。我仍然保存在一个var我的CBPeripheral中,我尝试连接到它。当我想要方法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();

}

0 个答案:

没有答案