简体   繁体   English

CoreBluetooth在预定时间的背景中

[英]CoreBluetooth in Background at Scheduled Time

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. 问题:我需要保持与BLE外设断开连接,但是将数据的数据发送到服务器以便尽可能频繁地进行处理,因为它可能是时间关键的。 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. 换句话说,我想经常连接并将同步的数据发送到API,同时在所有其他时间保持断开连接以节省电池寿命。

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. 尝试失败:将我的应用程序的Info.plist文件的UIBackgroundModes字段设置为bluetooth-central只能让我在连接时执行后台操作。 I want to remain disconnected, but reconnect at predefined intervals, as well as schedule an alarm from background mode. 我想保持断开连接,但是以预定义的间隔重新连接,以及从后台模式安排警报。

It's possible: I've noticed that the FitBit Flex app has an option in the settings to enable syncing in the background. 这是可能的:我注意到FitBit Flex应用程序在设置中有一个选项,可以在后台同步。 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. 我不确定它是否会在我的Flex范围内断开连接,但是判断它的电池尺寸如此之小 - 我猜它不会保持连接状态。

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

in: 在:

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

and adding fetch to UIBackgroundModes , which iOS then calls: 并将fetch添加到UIBackgroundModes ,然后iOS调用:

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

every 15-240 minutes (yeah, it varies a lot , but it's better than nothing). 每15-240分钟(是的,它变化很大 ,但它总比没有好)。 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 . 由于我将这些数据从BLE外设发送到服务器进行处理/存储,我假设这是一个合法的(AppStore值得)使用fetch

CAVEAT: application:performFetchWithCompletionHandler: will not be called until iOS establishes a user usage pattern for the app. CAVEAT: application:performFetchWithCompletionHandler:在iOS为应用程序建立用户使用模式之前不会被调用。 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. 换句话说,您需要在应用程序之前保持应用程序(不是删除它)大约24小时左右application:performFetch...方法被调用。 Boy, did it take a while to figure that out! 男孩,这需要一段时间来解决这个问题!

UPDATE: Apple has accepted my app that used this solution (approved May 2014). 更新: Apple已接受使用此解决方案的应用程序(2014年5月批准)。

You can't. 你不能。 A backgrounded app cannot start any transactions, it can only react to incoming requests (data notifications, connection events...) In the background the application is effectively not running and even in case of BLE events it has only 8 seconds to go back to sleep, otherwise it is completely terminated for breaking the policy. 后台应用程序无法启动任何事务,它只能响应传入的请求(数据通知,连接事件......)在后台,应用程序实际上没有运行,即使在BLE事件的情况下,它只有8秒钟返回睡觉,否则它会因违反政策而完全终止。

The only case when your app can stay alive for a while is when it uses the beginBackgroundTaskWithName:expirationHandler: API. 应用程序可以保持活动一段时间的唯一情况是它使用beginBackgroundTaskWithName:expirationHandler: API。 But even in that case it has only 10 minutes to complete or it gets killed. 但即使在这种情况下,它只有10分钟完成或它被杀死。

If you want to synchronize while in background, then the transaction must be started on the external peripheral's side. 如果要在后台同步,则必须在外围设备侧启动事务。 Easiest is to stay connected and send a notification to the central every once in a while. 最简单的方法是保持联系并每隔一段时间向中心发送一个通知。 That will wake it up and it can proceed with reading the characteristics it needs. 这将唤醒它,它可以继续阅读它所需的特性。 But there are several other ways to implement it. 但是还有其他几种方法可以实现它。 The final solution has to be designed to best meet your needs. 最终的解决方案必须旨在最好地满足您的需求。 If you have a concrete idea, then please submit it as a separate question. 如果您有一个具体的想法,那么请将其作为单独的问题提交。

https://developer.apple.com/hardwaredrivers/BluetoothDesignGuidelines.pdf https://developer.apple.com/hardwaredrivers/BluetoothDesignGuidelines.pdf

I'm pretty sure you guys have seen this, but just in case you haven't. 我很确定你们已经看过这个,但万一你还没有。 Perhaps on the BLE Peripheral end you can save power by increasing the connection interval (page 22). 也许在BLE外设端,您可以通过增加连接间隔来节省电量(第22页)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM