如何在后台以固定间隔重新启动CoreBluetooth管理器实例 [英] How to reboot CoreBluetooth manager instance at fixed interval in background

查看:95
本文介绍了如何在后台以固定间隔重新启动CoreBluetooth管理器实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用CoreBluetooth的iOS应用程序,我在应用程序BackGround中遇到一个问题.

I'm developing a iOS application of using CoreBluetooth and i have one problem in application BackGround.

通常,iOS应用程序不能在后台长期运行. (例如,按下HomeButton.切换其他应用程序),但是我的应用程序将使用Bluetooth LE配件"设置为BackGroundMode,因此我可以监视Background中的区域.
我在didEnterRegion中实现了startRangingBeaconsInRegion.
进入区域后,iOS 10秒钟后将开始并停止测距区域.

Generally, iOS application can't run long-term in Background. (e.g. pushing HomeButton. switching other application) But my application is set "Uses Bluetooth LE accessories" as BackGroundMode, so i can monitor region in Background.
And i implemented startRangingBeaconsInRegion in didEnterRegion.
When enter a region, Ranging region will be started and will be stopped after about 10 sec by iOS.

但是我想始终在后台使用测距.因为我的应用仅使用一个UUID来检测20多个信标(20表示startMonitoringForRegion的限制),所以我想知道一个区域中的信标如何.
(关于仅使用一个UUID的原因,请参阅此技巧.

But i want to always use ranging in Background. Because my app use only one UUID for detecting over 20 beacons(20 means limit of startMonitoringForRegion), and i want to know how beacons there are in one region.
(About The reason of using only one UUID, please see this tips.
iBeacon / Bluetooth Low Energy (BLE devices) - maximum number of beacons.)

因此,我正在考虑以固定间隔在后台重新启动CoreBluetooth管理器实例的方法.如果我可以交替执行didEnterRegion-> didRangeBeacons->重新启动-> didEnterRegion-> didRangeBeacons->重新启动-> ...,我可以按固定间隔检查区域中的信标.
也许我需要后台抓取...我稍后再尝试.
如果您知道这种方法是否可用,请告诉我.
如果您有任何建议,请告诉我,我会尝试的.

So I'm thinking the way of rebooting CoreBluetooth manager instance at fixed interval in background. if i can do alternately didEnterRegion -> didRangeBeacons-> reboot -> didEnterRegion -> didRangeBeacons -> reboot -> ..., i can check how beacon there are in the region at fixed interval.
Maybe i need background fetch... i'll try it later.
If you know about this way is available or not, please tell me that.
Or if you have any suggestions, please tell me, i'll try it.

更新了2014/03/07 17:45

BackGround提取将以不稳定的间隔触发.所以这不是解决方案...

BackGround fetch will fire at UNSTABLE interval. So This way isn't the solution...

推荐答案

您无需为后台模式设置该要求.您可以尝试applicationDidEnterBackground:调用startBackgroundTask

You do not need to set that requirement for background modes. You can try on applicationDidEnterBackground: call startBackgroundTask

- (void)startBackgroundTask {
    if(bgTask != UIBackgroundTaskInvalid){
        [[UIApplication sharedApplication] endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }
    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        NSLog(@"your time is over");
        //you can call start once again to get more time
    }];
}

bgTaskUIBackgroundTaskIdentifier bgTask;

我唯一担心的是,从我的观察结果来看,用户还有时间触摸背景以使屏幕发光(屏幕可以锁定).为了确保您的代码在后台运行,您可以设置一个计时器并定期在控制台上记录一条消息(例如,在后台执行[UIApplication sharedApplication].backgroundTimeRemaining或重新启动蓝牙时,哪个系统为您提供了剩余时间).据我所知,这种​​问题是很常见的方法.

My only worry would be that from my observations to get another time for background execution user has to touch the screen to make screen illuminating (screen can be locked). To make sure your code is working in background you can set up a timer and log a message on the console periodically (for example time remaining which system gave you on the background execution [UIApplication sharedApplication].backgroundTimeRemaining or restarting bluetooth). As far as I know it is quite common approach to that kind of problems.

这篇关于如何在后台以固定间隔重新启动CoreBluetooth管理器实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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