CoreBluetooth在后台在计划时间 [英] CoreBluetooth in Background at Scheduled Time

查看:129
本文介绍了CoreBluetooth在后台在计划时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我需要保持从BLE外围断开,但发送数据是数据处理服务器尽可能多的,因为它可能是时间关键。换句话说,我要常常每次连接和同步数据发送到API,而在其他时间断开,其余为节省电池寿命。

Problem: I need to remain disconnected from a BLE peripheral but send data it's data to a server for processing as often as possible, as it is potentially time-critical. In other words, I want to connect every so often and send the synced data to an API, while remaining disconnected at all other times to save battery life.

失败的尝试:我的应用程序的Info.plist文件中的 UIBackgroundModes 字段设置为蓝牙中央只有当我连给我后台执行。我想保持断开,但是在predefined间隔重新连接,以及时间表从后台模式的警报。

Failed Attempt: Setting the UIBackgroundModes field of my app's Info.plist file to bluetooth-central only gives me background execution while I am connected. I want to remain disconnected, but reconnect at predefined intervals, as well as schedule an alarm from background mode.

这是可能的:我已经注意到,FitBit Flex应用程序在设置一个选项来启用同步的背景。我不知道,如果它曾经从我的Flex断开,而它在范围内,但其判断这么小我倒是全输,其电池的大小,我猜它不保持连接。

It's possible: I've noticed that the FitBit Flex app has an option in the settings to enable syncing in the background. I am not sure if it ever disconnects from my Flex while it is in range, but judging its so-small-I'd-lose-it battery size, I'm guessing it does not remain connected.

推荐答案

我知道,我已经接受了答案本(对不起!),但我已经找到了解决办法:

I know I've already accepted an answer for this (sorry!), but I've found a solution:

[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:60*5]; // Every 5 minutes, minimum

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

和增加 UIBackgroundModes ,它的iOS然后调用:

and adding fetch to UIBackgroundModes, which iOS then calls:

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler

每15-240分钟(是的,这一个变化的很多的,但它总比没有更好)。每次我调用以执行一个取指令,我连接到​​外围设备,同步和它的数据发送到服务器,然后断开。自从我送从BLE外设这些数据进行处理/存储的服务器,我是presuming,这是合法的(AppStore的堪)使用取。

every 15-240 minutes (yeah, it varies a lot, but it's better than nothing). Every time I am called to perform a fetch, I connect to the peripheral, sync and send its data to the server, then disconnect. Since I'm sending this data from the BLE peripheral to a server for processing/storage, I'm presuming that this is a legitimate (AppStore worthy) use of fetch.

警告: 应用程序:performFetchWithCompletionHandler:将的的被调用,直到iOS的建立用户使用模式的应用。换句话说,你需要的应用程序之前,要保持周围的应用程序(而不是删除)约24小时左右:performFetch ... 方法被调用。男孩,没有它需要一段时间才能找出答案!

CAVEAT: application:performFetchWithCompletionHandler: will not be called until iOS establishes a user usage pattern for the app. In other words, you need to keep the app around (not delete it) for about 24 hours or so before the application:performFetch... method gets called. Boy, did it take a while to figure that out!

更新:苹果公司已经接受了我的应用程序,使用该解决方案(已批准2014年5月)

UPDATE: Apple has accepted my app that used this solution (approved May 2014).

这篇关于CoreBluetooth在后台在计划时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆