我有 xamarin 项目。我想将蓝牙按钮与应用程序配对,并在挂起状态下保持连接事件。我成功订阅了代表前台状态点击的特征事件。主要用例是处理处于挂起状态的事件并将数据发送到服务器。
我阅读了文档 here ,但我在恢复 CBCentralManager 时遇到困难,特别是将这些方法转换为 Xamairn.iOS。
选择状态保护和恢复
myCentralManager =
[[CBCentralManager alloc] initWithDelegate:self queue:nil
options:@{ CBCentralManagerOptionRestoreIdentifierKey:
@"myCentralManagerIdentifier" }];
重新实例化您的中央和外围管理器
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSArray *centralManagerIdentifiers =
launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];
...
实现适当的恢复委托(delegate)方法
- (void)centralManager:(CBCentralManager *)central
willRestoreState:(NSDictionary *)state {
NSArray *peripherals =
state[CBCentralManagerRestoredStatePeripheralsKey];
...
问题:
应用程序是否处理处于挂起状态的事件? (技术背景)
应用恢复后我是否需要重新连接设备?
最佳答案
我想你可以在文档中找到答案。
1.Does app handle the events in suspended state? (technically in backgrounded)
您需要在info.plist
中启用Core Bluetooth 后台执行模式
,以确保您的应用程序在后台继续运行。
此外,在 iOS 10.0 上或之后链接的 iOS 应用程序必须在其 Info.plist
文件中包含它需要访问的数据类型的使用说明 key ,否则它将崩溃。 InfoPlistKeyReference
您可以阅读:Core Bluetooth Background Execution Modes
2.Do I have to reconnect the device after app gets restored?
如果你恢复
这里意味着你重新启动你的应用程序
,我认为你需要重新连接,因为它会在应用程序终止后失去连接。
If you gets restored
这里的意思是 enter foreground
from background
,我认为如果启用了后台模式,则不需要重新连接您的应用仍在后台运行。
关于ios - 如何在 iOS 上恢复蓝牙连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54570390/