在iOS 10以上版本中,有什么方法可以可靠地唤醒应用程序 [英] In iOS 10+, is there ANY way to RELIABLY wake up an app

查看:143
本文介绍了在iOS 10以上版本中,有什么方法可以可靠地唤醒应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经参加了3个月多了,把头发拔了。因此,请不要回答初学者的答案。

我认为终止是在用户刷卡时任务管理器中的应用程序,或者打开/关闭外围设备并且该应用程序已经死了

我需要与健康相关的重要BT外围设备数据(由BT设备)保持在应用程序中,因此我需要保持一致的连接或能够唤醒应用程序并处理数据的能力。我知道这是很多问题,所以我试图找到关于此问题的最新理解或解决方案。我读了很多文章和S.O.关于此的文章,所以我知道Core Bluetooth充其量是不可靠的。我知道总体概念是片状的,自2010年以来人们一直在说这是不可能的。但是,iOS方面有很多变化,所以我希望某些事情会有所变化。

I need important health related BT peripheral data (recorded by BT device) maintained in the app so I need a consistent connection or the ability to wake the app back up and handle the data. I know this is asked a lot so I am trying to find the most current understanding or solutions to this problem. I have read sooo many articles and S.O. posts on this so I know Core Bluetooth is un-reliable at best. I know the general concept is flaky and people have been saying since 2010 its not possible. However, lots keeps changing in iOS so I was hoping something would have changed.

要明确:

To be clear:

BT唤醒up很棒,但是它确实不可靠,所以...我将采取任何一种可靠的唤醒方式(位置,音频,BT等。。。由于我已连接/配对到BT设备,因此不是iBeacon)。如果我必须唤醒发生在位置或音频上的唤醒,然后以某种方式快速从外围设备获取数据,我会接受!

BT wake up would be great but it's really not been reliable, so... I will take ANY kind of reliable wake up (location, audio, BT, etc... NOT iBeacon though since I am connected/paired to BT device). If I have to "hack" the wake up to happen on location or audio and then quickly get the data from the peripheral somehow, I will take it!

(请跳过此操作,如果您不在乎或不适用)


  • 在info.plist中打开背景中央模式

  • 使用完整状态还原,即可以说,这段代码...

  • Background central mode turned on in info.plist
  • Using full state restoration, that is to say, this code...

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self
                                                           queue:nil
                                                             options:@{CBCentralManagerOptionShowPowerAlertKey: @(YES),
                                                                       CBCentralManagerOptionRestoreIdentifierKey:@"MyDevice"}];

要注册标识符键和此代码...

To register the identifier key and this code...

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"launch options found: %@", launchOptions);
    NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];

    NSLog(@"central managers found in launch options: %@", centralManagerIdentifiers);
    [self triggerLocalNotification:[NSString stringWithFormat:@"central managers found in launch options: %@", centralManagerIdentifiers]];

    if([centralManagerIdentifiers count] > 0) {
        for(NSString *identifier in centralManagerIdentifiers) {
            if([identifier isEqualToString:@"MyDevice"]) {
                [self triggerLocalNotification:[NSString stringWithFormat:@"Identifier found: %@", identifier]];
                self.bluetoothManager = [BluetoothMgr sharedInstance];
            }
        }
    }

    return YES;
}

- (void)centralManager:(CBCentralManager *)central
  willRestoreState:(NSDictionary<NSString *,id> *)state {

    NSLog(@"************** RESTORED STATE BT **************");
    [self triggerCustomLocalNotification:@"************** RESTORED STATE BT **************"];

    NSLog(@"central manager object: %@", central);
    NSLog(@"state dictionary: %@", state);

    [self triggerCustomLocalNotification:[NSString stringWithFormat:@"state dictionary: %@", state]];


    NSArray *restoredPeripherals = [state objectForKey:@"CBCentralManagerRestoredStatePeripheralsKey"];

    self.centralManager = central;
    self.centralManager.delegate = self;

    if([restoredPeripherals count] > 0) {
        for(CBPeripheral *peripheral in restoredPeripherals) {
            if([peripheral.name rangeOfString:@"mybox-"].location != NSNotFound) {
                NSLog(@"Restoring mybox Box: %@", peripheral);
                [self triggerCustomLocalNotification:[NSString stringWithFormat:@"Peripheral was found in WILL RESTORE STATE! it was: %@", peripheral]];

                self.myPeripheral = peripheral;
                self.myPeripheral.delegate = self;

                [self connectToDevice];

                return;
            }
        }
    }
}

到恢复中央管理器状态。仅当该应用被iOS终止或状态更改时,此功能才有效。用户终止应用程序时不起作用。

To restore the central manager state. This only works when the app is killed by iOS or the state is changed. Does not work when the user kills the app.

订阅设备中的通知特性(我创建了此自定义特性,并且可以完全控制设备的编程)...确实有效很好,但并不总是能唤醒应用程序。虽然在后台运行良好。只是没有终止。

Subscribing to a notifying characteristic in the device (I made this custom characteristic and I have full control over the programming of the device) ... this works really well but does not always wake the app up. Works well in background though. Just not terminated.

推荐答案

终于解决了这个问题!解决方案是在我的解决方案中使用2个蓝牙芯片。一种芯片是专用于BT连接的配对/验证/绑定设备,另一种芯片是专用的iBeacon广告客户。使用此解决方案,我既可以在需要时唤醒应用程序(通过随意重启iBeacon芯片),也可以连接BT加密所需的特性。

Finally solved this problem! The solution was to use 2 Bluetooth chips in my solution. One chip to be a dedicated BT-Connected Paired/Auth/Bonded device and the other to be a dedicated iBeacon advertiser. With this solution I was able to both, wake up the app whenever I want (by power cycling the iBeacon chip at will) and connecting for BT encryption required characteristics.

使用 CLLocationManager 类的 didEnterRegion 方法,在后台,我可以启动蓝牙管理器...在后台连接到设备,然后通过先前配对的连接成功检索数据。

Using the didEnterRegion method of the CLLocationManager class, in the background, I can start up the bluetooth manager... connect to the device in the background and then retrieve data successfully over a previously paired connection.

更新:作为附带说明,很高兴提到,尽管iBeacon在后台唤醒应用程序方面相当可靠,但仅 didEnterRegion 方法会立即发生找到或打开iBeacon时。 didExitRegion 方法在关闭iBeacon或它不在范围内后,平均大约需要30秒触发我。

UPDATE: as a side note, it's good to mention that while the iBeacon is fairly reliable in waking up the application in the background, only the didEnterRegion method happens immediately when the iBeacon is found or turned on. The didExitRegion method takes me (on average) about 30 seconds to fire after I turn off the iBeacon or it is no longer in range.

这篇关于在iOS 10以上版本中,有什么方法可以可靠地唤醒应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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