Swift - 暂停后应用程序BLE重新连接/扫描

时间:2017-04-24 20:57:45

标签: ios swift bluetooth-lowenergy background-process suspend

我目前正在编写一个通过BLE连接到外部设备的应用程序。当应用处于前台时,所有操作都很好.....包括连接,获取数据和重新连接(在设备超出范围的情况下)通过我写的重新连接协议。该应用程序在后台运行时也能正常运行,但BLE连接仍然存在。

但是,应用程序无法运行的唯一实例是应用程序是否后台运行,然后BLE设备超出范围。一旦连接断开,几秒钟后iOS就会暂停应用程序,我编写的代码都不会继续运行......即使我将设备带回范围内也是如此。恢复功能的唯一方法是将应用程序重新置于前台。 (注意:我有info.plist文件和为centralManager后台功能配置的所有其他设置)

我已经阅读了一些文档,而这似乎归结为没有正确实现状态保存/恢复代码。我继续实施" willRestoreState"和" didUpdateState"命令,但在处于后台模式时,应用仍然无法重新连接到设备。

我在下面展示了一些相关代码,包括willRestoreState,didUpdateState和didDisconnect方法。任何想法或建议?谢谢!

//define service+characteristic UUIDS
let serviceUUID                          = CBUUID(string: "xxxxxxxxx")
let streamingCharacteristicUUID          = CBUUID(string: "xxxxxxxxx")

//Local dictionary of UUIDs for connected devices (the ble code updates this var with each connected device)
var devicesUniqueId:[UUID:String] = [UUID:String]()

//Local dictionary of connected peripherals, with respect to each of their UUIDS (the ble code updates this var with each connected device)
var sensorPeripheral = [UUID:CBPeripheral]()



///restoreState function
 func centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any]) {

    if let peripheralsObject = dict[CBCentralManagerRestoredStatePeripheralsKey] {

        let peripherals = peripheralsObject as! Array<CBPeripheral>       
        print ("starting restorestate code")
        if peripherals.count > 0 {
            for i in 0 ..< peripherals.count {
                print ("starting restorecheck")

//Check if the peripheral exists within our list of connected peripherals, and assign delegate if it does
                if self.devicesUniqueId.keys.contains(peripherals[i].identifier) {
                    peripherals[i].delegate = self
                }
            }
        }
    }
}



 func centralManagerDidUpdateState(_ central: CBCentralManager)
{ 
    if central.state != .poweredOn
    {
        return
    }
     self.startScanning()

    //////Preservation + Restoration code////////


//Iterate through array of connected UUIDS
    let keysArray = Array(self.patchDevicesUniqueId.keys)
        for i in 0..<keysArray.count {

//Check if peripheral exists for given UUID
           if let peripheral = self.sensorPeripheral[keysArray[i]] {              
            print("peripheral exists")       

//Check if services exist within the peripheral
            if let services = peripheral.services {           
                 print("services exist")   

//Check if predefined serviceUUID exists within services 
                if let serviceIndex = services.index(where: {$0.uuid == serviceUUID}) {                 
                     print("serviceUUID exists within services")          
                    let transferService = services[serviceIndex]
                    let characteristicUUID = streamingCharacteristicUUID        

//Check if predefined characteristicUUID exists within serviceUUID
                    if let characteristics = transferService.characteristics {                    
                         print("characteristics exist within serviceUUID")                           

                        if let characteristicIndex = characteristics.index(where: {$0.uuid == characteristicUUID}) {                               
                             print("characteristcUUID exists within serviceUUID")                      
                            let characteristic = characteristics[characteristicIndex]  

//If characteristicUUID exists, begin getting notifications from it
                            if !characteristic.isNotifying {                                   
                                 print("subscribe if not notifying already")
                                peripheral.setNotifyValue(true, for: characteristic)
                            }
                            else {                                    
                                 print("invoke discover characteristics")    
                            peripheral.discoverCharacteristics([characteristicUUID], for: transferService)
                            }                               
                        }                                                        
                    }                        
                }
                else {
                    print("invoke discover characteristics")
                    peripheral.discoverServices([serviceUUID])
                }                 
            }            
        }
    }
}



//didDisconnect method to handle a connect command issue
    func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?)
{


//commented out unnecessary code

    self.removePeripheralData(peripheral: peripheral)

    if(sensorCount>0){
        sensorCount -= 1
    }

}


//removePeripheralData function used in didDisconnect
 func removePeripheralData ( peripheral: CBPeripheral) {

   //Commented out unnecessary code

            //Issue reconnect command
            print ("issuing reconnect command")
              centralManager.connect(peripheral, options: nil)

     //Commented out unnecessary code

    handleDidRemoveDevice()
}

0 个答案:

没有答案