[英]In iOS 10+, is there ANY way to RELIABLY wake up an app
I have been at this over 3 months and pulling my hair out. 我已经超过3个月了,把头发拔了。 So please don't respond with beginner answers.
因此,请不要以初学者的答案回答。
I consider terminated to be when user swiped the app in task manager or when the peripheral is turned on/off and the app was already dead 我认为终止是因为用户在任务管理器中刷过应用程序或外围设备打开/关闭且应用程序已死
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. 我需要在应用程序中维护重要的与健康相关的BT外围设备数据(由BT设备记录),因此我需要一致的连接或能够唤醒应用程序并处理数据的功能。 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 SO posts on this so I know Core Bluetooth is un-reliable at best.
我已经读了很多文章和相关文章,所以我知道Core Bluetooth充其量是不可靠的。 I know the general concept is flaky and people have been saying since 2010 its not possible.
我知道总体概念是片状的,自2010年以来人们一直在说这是不可能的。 However, lots keeps changing in iOS so I was hoping something would have changed.
但是,iOS方面有很多变化,所以我希望某些事情会有所变化。
To be clear: 要清楚:
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). BT唤醒会很棒,但实际上并不可靠,所以...我将采取任何一种可靠的唤醒方式(位置,音频,BT等...,因为我已连接/配对到BT设备,所以不是iBeacon) 。 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!
如果必须“唤醒”发生在位置或音频上的唤醒,然后以某种方式快速从外围设备获取数据,我将接受!
(SKIP THIS IF YOU DON'T CARE OR IS NOT APPLICABLE) (如果您不关心或不适用,请跳过此步骤)
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; } } } }
To restore the central manager state. 恢复中央管理器状态。 This only works when the app is killed by iOS or the state is changed.
仅当该应用被iOS终止或状态更改时,此功能才有效。 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.
只是没有终止。
Finally solved this problem! 终于解决了这个问题! The solution was to use 2 Bluetooth chips in my solution.
解决方案是在我的解决方案中使用2个蓝牙芯片。 One chip to be a dedicated BT-Connected Paired/Auth/Bonded device and the other to be a dedicated iBeacon advertiser.
一种芯片是专用于BT连接的配对/验证/绑定设备,另一种芯片是专用的iBeacon广告客户。 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.
使用此解决方案,我既可以在需要时唤醒应用程序(通过随意重启iBeacon芯片),也可以连接BT加密所需的特性。
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. 在
didEnterRegion
使用CLLocationManager
类的didEnterRegion
方法,我可以启动蓝牙管理器...在后台连接到设备,然后通过先前配对的连接成功检索数据。
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. 更新:作为附带说明,值得一提的是,尽管iBeacon在后台唤醒应用程序方面相当可靠,但是在找到或打开iBeacon时,只有
didEnterRegion
方法会立即发生。 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. 在我关闭iBeacon或它不在范围内之后,
didExitRegion
方法平均要花30秒触发我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.