2016-08-15 16 views
0

これは、アプリケーションがユーザーによって選択されたBTに接続されているときに何らかの処理を行うことができるプライベートアプリケーション用です。 https://github.com/michaeldorner/BeeTee経由でbluetoothmanager.frameworkを使用していますが、残念ながら、アプリケーションがバックグラウンドにある場合は、アプリケーションを起動することができないようです。 可能ですか?我々はでそれを行う方法を手がかりにしてうれしいです。アプリケーションは永遠にバックグラウンドで公開されています - プライベートアプリ

私たちは、膨大なバッテリ消費がなくてもユーザーがそれを殺さないうちに、バックグラウンドでアプリを稼動させる方法を見つける必要があります。

現在、私たちはこのアプリを保つこの作品を使用しています。生きている間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/Bluetoothを使用している場合は、バッテリーを絶えず消費する必要があります(BLEはバッテリーの使用量を減らします)。 – Raptor

+0

私は具体的なことは許されませんが、現在、 BT。したがって、通常の使用での現在の作業は約1%/ 2時間のバッテリー消費ですが、OSがしばらくしてアプリケーションをスリープ状態にするので、この解決策は実行可能ではありません –

答えて

0

Bluetooth 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のようないくつかのことを処理するために必要なすべてこのほか、など...しかし、それは行く一つの方法です。

関連する問題