gpt4 book ai didi

ios - 当外设需要特殊 ACK 时,CoreBluetooth 和状态保存和恢复

转载 作者:行者123 更新时间:2023-11-29 05:47:17 25 4
gpt4 key购买 nike

我正在使用一个蓝牙外围设备,在连接到它之后,会来回发送一个 ACK​​,然后它才能真正向我发送所需的数据。

流程是这样的:

  1. 探索外围设备
  2. 连接到外围设备
  3. 发现服务和特点
  4. 聆听特定特征的最新动态
  5. 外设向此特征发送特殊消息
  6. 然后应用程序向外围设备发送 ACK
  7. 外围设备向我发送应用程序所需的数据

我已经在状态保存和恢复的协议(protocol)方法中实现了所需的逻辑centralManager(_ central: CBCentralManager, willRestoreState dict: [String : Any])

问题 1:

当应用程序处于后台并且 iOS 决定接管蓝牙通信(出于资源原因)时,iOS 将如何执行步骤 5、6 和 7?

因为如果不能,那么外围设备将无法发送应用程序在第 7 步中所需的数据。

问题2:

在文档中,我读到 iOS 可能会启动您的应用程序几秒钟。请问viewDidLoad在这种情况下我的根 ViewController 的方法会被执行吗?这就是我实例化 CBCentralManager 的地方

<小时/>

我找到的许多在线资源中的一些:

Core Bluetooth Background Processing for iOS Apps

Zero to BLE on iOS – Part Three

最佳答案

Paulw11 的这两条评论帮助我了解了发生的事情:

No, that doesn’t happen. If you have a pending discovery, a pending “connect” or you have an active notify on a characteristic then iOS will relaunch your app so that it can handle the discovery, the connection or the notification. iOS can’t do this on your behalf; it doesn’t know what you want to do. – Paulw11

<小时/>

Having your Bluetooth object attached to a view controller is a bad idea if you want to perform operations in the background. I suggest a singleton or an object owned by your app delegate – Paulw11

<小时/>

首先,我错误地假设当我的应用程序在后台运行且 iOS 必须终止它时,iOS 将尝试代表我的应用程序处理所有蓝牙通信。现实情况是,iOS 会在一段有限的时间内以后台模式启动应用程序,以便您可以运行恢复代码。

最后,将逻辑放在viewDidLoad中我的 ViewController 是错误的。我创建了一个BluetoothManager类,我在 application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool 中实例化了它我的方法AppDelegate

关于ios - 当外设需要特殊 ACK 时,CoreBluetooth 和状态保存和恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56017194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com