ios - 如何在 iOS 上恢复蓝牙连接

标签 ios xamarin xamarin.forms xamarin.ios

我有 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. 应用程序是否处理处于挂起状态的事件? (技术背景)

  2. 应用恢复后我是否需要重新连接设备?

最佳答案

我想你可以在文档中找到答案。

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/

相关文章:

android - 定点数学 - 从浮点安全到总是产生相同结果的转换?

c# - 未找到与 RestoreAssemblyResources AssemblyName 元数据匹配的引用

c# - Xamarin.Forms - View 高度等于宽度

android - Xamarin 使用 OKHttpNetworkHandler 和 AFNetworkHandler 的引用

android - Xamarin 和 Android - 退出方法时崩溃

android - 尝试发布到 Google Play 应用商店时出现错误 403

c# - 无法将位图转换为字节 []

ios - 尝试启动 YouTube 应用的 iOS 网络应用出现问题

ios - UICollectionViewCell 中的 AVPlayer 播放声音但不显示视频

ios - 断点 2.1 错误解析/Facebook iOS SDK