2016-08-15 27 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保持始终在后台的位置,等等......但这是一条路。