• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 在 iOS 10+ 中,有什么方法可以可靠地唤醒应用程序

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:37:57 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我已经做了 3 个多月了,把头发拔掉了。所以请不要回答初学者的答案。

我想知道,在 2017 年使用 iOS 10+ 时,是否有任何方法可以将应用程序从终止状态唤醒……最好是通过蓝牙外围设备……但我会尽我所能!

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

我需要在应用程序中维护重要的与健康相关的 BT 外围设备数据(由 BT 设备记录),因此我需要一致的连接或唤醒应用程序备份和处理数据的能力。我知道这个问题很多,所以我试图找到这个问题的最新理解或解决方案。我已经阅读了很多文章和 S.O.在此发布帖子,因此我知道 Core Bluetooth 充其量是不可靠的。我知道一般概念是不稳定的,自 2010 年以来人们一直在说这是不可能的。然而,iOS 中的很多东西都在不断变化,所以我希望有些东西会发生变化。

要明确:

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

我试过了:

(如果您不关心或不适用,请跳过此)


  • 在 info.plist 中开启了后台集中模式
  • 使用全状态恢复,也就是这段代码……

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

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

    - (BOOL)applicationUIApplication *)application willFinishLaunchingWithOptionsNSDictionary *)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)centralManagerCBCentralManager *)central
      willRestoreStateNSDictionary<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"eripheral was found in WILL RESTORE STATE! it was: %@", peripheral]];
    
                    self.myPeripheral = peripheral;
                    self.myPeripheral.delegate = self;
    
                    [self connectToDevice];
    
                    return;
                }
            }
        }
    }
    

    恢复中央管理器状态。这仅在应用程序被 iOS 终止或状态更改时有效。当用户杀死应用程序时不起作用。

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

  • 尝试在终止时完全断开连接,以便我可以使用 iBeacon 唤醒备份...太多的箍,最终它根本无法可靠地工作。
  • 重要的位置更新...极其不可靠
  • 录音...开始录音时没有方法触发(我无论如何都能找到)或在录音时间歇性触发的方法



Best Answer-推荐答案


终于解决了这个问题!解决方案是在我的解决方案中使用 2 个蓝牙芯片。一个芯片是专用的 BT-Connected Paired/Auth/Bonded 设备,另一个是专用的 iBeacon 广告商。有了这个解决方案,我可以随时唤醒应用程序(通过随意重启 iBeacon 芯片)和连接 BT 加密所需的特性。

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

更新: 作为旁注,值得一提的是,虽然 iBeacon 在后台唤醒应用程序方面相当可靠,但只有 didEnterRegion 方法会立即发生当 iBeacon 被发现或打开时。 didExitRegion 方法在我关闭 iBeacon 或它不再在范围内后(平均)需要大约 30 秒才能触发。

关于ios - 在 iOS 10+ 中,有什么方法可以可靠地唤醒应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45738824/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap