2016-08-15 66 views
0

這是一個私人應用程序,它能夠在應用程序連接到用戶選擇的BT時執行一些操作。 我們使用bluetoothmanager.framework通過https://github.com/michaeldorner/BeeTee不幸的是,它似乎不能喚醒應用程序,如果它在後臺。 可能嗎?我們很高興能有任何線索如何處理應用程序永遠在活動背景 - 私人應用程序

所以我們需要找到一種方法來保持應用程序在後臺保持活躍狀態​​,而用戶在沒有大量電池消耗的情況下不會殺死它。

目前我們使用這項工作,保持應用程序。活躍於2 & 4小時以內。 (不夠明顯,但與這一個應用程序消耗無:在2小時內〜1%)

背景模式:

  • VOIP
  • 位置更新
  • 背景取
  • 音頻播放

使用的代碼:

- (void)applicationDidEnterBackground:(UIApplication *)application { 

     if([[NSUserDefaults standardUserDefaults]objectForKey:@"account"]){ 

      _background_task = [application beginBackgroundTaskWithExpirationHandler:^ { 
       NSLog(@"cleanup code for end of background allowed running time"); 
       [application endBackgroundTask: _background_task]; 
       _background_task = UIBackgroundTaskInvalid; 
      }]; 

      // run background loop in a separate process 
      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       NSLog(@"start of background loop"); 
       while (TRUE) 
       { 
        NSTimeInterval remaining = [[UIApplication sharedApplication] backgroundTimeRemaining]; 
        // background audio resets remaining time 
        if (remaining < 60) { 

         [self playSpeech:@"up" andVolume:0.0]; 
        } 
        [NSThread sleepForTimeInterval:1]; //wait for 1 sec 
       } 
       NSLog(@"end of background loop"); 
       [application endBackgroundTask: _background_task]; 
       _background_task = UIBackgroundTaskInvalid; 
      }); 

      [[Detector singleton] startDetection]; 
     } 
    } 

如何保持在後臺活着應用程序,而不破壞用戶的電池任何線索?

+0

*使應用程序保持活動狀態而不損壞用戶電池*?這是矛盾的。如果後臺任務不做任何事情,則不會耗費電池壽命。但是,如果您使用GPS /藍牙,它必須不斷消耗電池(雖然BLE會減少電池的使用) – Raptor

+0

我不被允許具體,但目前有一些條件可以在連接到「某些東西」之後BT。所以目前正常使用的工作約爲電池消耗的1%/ 2小時左右,但由於操作系統在一段時間後強制休眠應用程序,因此此解決方案不可行 –

回答

0

如果你正在使用藍牙4和BTLE,那麼一種解決方案是註冊(如你所說)的背景位置更新。

在CoreLocation的幫助下,您可以設置CLLocationManager來監視最多20個不同的區域。區域顯然可以由地理/ GPS區域構成,但也可以由iBeacon區域構成,稱爲CLBeaconRegion。因此,如果你實現這個設計,當你進入和/或退出CLBeaconRegion時,你的應用可能會被喚醒。最重要的是,如果您的應用程序在註冊區域監控後遇害,iOS會在邊界超出時將其喚醒。

斯威夫特2例如:

// setup your CLLocationManager : 

var locManager: CLLocationManager = CLLocationManager() 
locManager.delegate = self 
locManager.requestAlwaysAuthorization() 

// Register for region monitoring : 

let uuid: NSUUID = NSUUID(UUIDString: "BF89DEDF-35E1-93A2-D316-2381E10A28EF")! 
let majorValue: CLBeaconMajorValue = CLBeaconMajorValue(UInt16(63468)) 
let minorValue: CLBeaconMinorValue = CLBeaconMajorValue(UInt16(13575)) 
let beaconRegion: CLBeaconRegion = CLBeaconRegion(proximityUUID: uuid, major: majorValue, minor: minorValue, identifier: "my-beacon") 
self.locManager.startMonitoringForRegion(beaconRegion) 

// from there implement the CLLocationManagerDelegates to get updates while in forground : 

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) 
{ 
    print("locationManager didEnterRegion = \(region)") 
    if let beaconRegion = region as? CLBeaconRegion 
    { 
     // do your stuff 
    } 
} 

func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) 
{ 
    print("locationManager didExitRegion = \(region)") 
    if let beaconRegion = region as? CLBeaconRegion 
    { 
     // do your stuff 
    } 
} 

// and to know if iOS woke you up when your app was asleep or terminated, you need to check the presence of the UIApplicationLaunchOptionsLocationKey in your AppDelegate : 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    if launchOptions?[UIApplicationLaunchOptionsLocationKey] != nil 
    { 
     // here you know you've been woken up about region crossing... 
     // you need to restart your region monitoring as explained in the documentation*, and do your stuff... 
    } 
} 

* UIApplicationLaunchOptionsLocationKey doc here

這是一個非常簡單的代碼,除了這一切,你需要處理一些事情,如用戶aknowledgment保持始終在後臺的位置,等等......但這是一條路。