在后台将BLE设备数据发送到服务器

问题描述 投票:0回答:1

我开发了一个从BLE设备读取数据并将此数据发送到MQTT代理(服务器)的应用程序。但是当应用程序进入后台时,数据发送在3分钟后停止(我使用后台任务)。我怎么能增加这个时间。或者也许有一个官方机制,Apple推出并且不会拒绝App Store上的确认步骤,从BLE读取数据并在不受时间限制的后台将数据发送到服务器?

我的背景任务:

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

@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

这里有谁遇到这个问题?最好的问候,安东。

objective-c bluetooth-lowenergy mqtt core-bluetooth
1个回答
0
投票

总而言之,beginBackgroundTaskWithExpirationHandler将不起作用 - 使用Core Bluetooth Background Processing for iOS Apps。彻底阅读它,因为如果你错过了一点,你很可能不会得到错误 - 它会无声地失败。

你需要首先将它添加到你的Info.plist

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

特别注意题为Use Background Execution Modes Wisely的部分,具体如下:

被唤醒后,应用程序大约需要10秒钟才能完成任务。理想情况下,它应该尽快完成任务并允许自己再次暂停。花费太多时间在后台执行的应用程序可能会被系统限制或被杀死。

虽然MQTT非常棒,但您可能需要切换到NSURLSessionUploadTask,因为上传在操作系统的其他地方异步发生,所以时间不会计入10秒。

© www.soinside.com 2019 - 2024. All rights reserved.