在后台将BLE设备数据发送到服务器 [英] Sending BLE device data to server in background

查看:193
本文介绍了在后台将BLE设备数据发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个从BLE设备读取数据并将此数据发送到MQTT代理(服务器)的应用程序.但是,当应用程序进入后台时,数据发送在3分钟后停止(我使用后台任务).我该如何增加这个时间.或者,也许有一种正式的机制,苹果会推广并且不会拒绝App Store上的确认步骤,以便从BLE中读取数据并将其发送到不受时间限制的后台服务器中?

I develop an application that reading data from BLE device and send this data to MQTT broker (server). But when application entering to the background, data sending stopped after 3 minutes (I use Background Tasks). How I can increase this time. Or maybe there is an official mechanism, that Apple promotes and will not reject on the confirmation step on App Store, for reading data from BLE and sending this data to the server in the background that not limited by time?

我的后台任务:

AYBackgroundTask.h

AYBackgroundTask.h

@interface AYBackgroundTask : NSObject

@property (assign) UIBackgroundTaskIdentifier identifier;
@property (strong, nonatomic) UIApplication *application;

+ (void)run:(void(^)(void))handler;

- (void)begin;
- (void)end;

@end

AYBackgroundTask.m

AYBackgroundTask.m

@implementation AYBackgroundTask

+ (void)run:(void(^)(void))handler {
  AYBackgroundTask *task = [[AYBackgroundTask alloc] init];
  [task begin];
  handler();
}

- (void)begin {
  self.identifier = [self.applicationn beginBackgroundTaskWithExpirationHandler:^{
    [self end];
  }];
}

- (void)end {
  if (self.identifier != UIBackgroundTaskInvalid) {
    [self.application.application endBackgroundTask:self.identifier];
  }
  self.identifier = UIBackgroundTaskInvalid;
}

@end

这里有谁面临这个问题? 最好的祝福, 安东.

There is here who faced with this problem? Best regards, Anton.

推荐答案

总结一下,beginBackgroundTaskWithExpirationHandler将不起作用-使用

To summarise, beginBackgroundTaskWithExpirationHandler will not work - use Core Bluetooth Background Processing for iOS Apps. Read it thoroughly as you likely won't get errors if you miss a bit out - it will silently fail.

您需要先将其添加到Info.plist:

<key>UIBackgroundModes</key>
<array>
    <string>bluetooth-central</string>
</array>

特别注意标题为

Pay special attention to the section entitled Use Background Execution Modes Wisely, specifically:

应用唤醒后,大约有 10秒才能完成任务.理想情况下,它应尽快完成任务并允许其再次挂起.在后台执行的时间过长的应用可能会被系统限制或终止.

Upon being woken up, an app has around 10 seconds to complete a task. Ideally, it should complete the task as fast as possible and allow itself to be suspended again. Apps that spend too much time executing in the background can be throttled back by the system or killed.

虽然MQTT很棒,但您可能需要切换为 NSURLSessionUploadTask 上传是在操作系统的其他位置异步进行的,因此时间不会占用您的10秒.

While MQTT is fantastic, you may need to switch to NSURLSessionUploadTask as the upload happens asynchronously elsewhere in the OS so the time won't count against your 10 seconds.

这篇关于在后台将BLE设备数据发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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