繁体   English   中英

如何在iOS上还原蓝牙连接

[英]How to restore bluetooth connection on iOS

我有xamarin项目。 我想将蓝牙按钮与应用配对,并保持连接处于挂起状态。 我成功订阅了代表在前景状态下点击的特征事件。 主要用例是在挂起状态下处理事件并将数据发送到服务器。

我在这里阅读了文档,但是在恢复CBCentralManager的实现方面遇到了困难,尤其是将这些方法转换为Xamairn.iOS。

选择加入国家保护和恢复

myCentralManager =
    [[CBCentralManager alloc] initWithDelegate:self queue:nil
     options:@{ CBCentralManagerOptionRestoreIdentifierKey:
     @"myCentralManagerIdentifier" }];

重新实例化您的中央和外围设备经理

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSArray *centralManagerIdentifiers =
        launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];
    ...

实现适当的还原委托方法

- (void)centralManager:(CBCentralManager *)central
  willRestoreState:(NSDictionary *)state {

NSArray *peripherals =
    state[CBCentralManagerRestoredStatePeripheralsKey];
...

问题:

  1. 应用程序是否在挂起状态下处理事件? (技术上是有背景的)

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

我认为您可以在文档中找到答案。

1.应用程序是否在挂起状态下处理事件? (技术上是有背景的)

您需要在info.plist启用Core Bluetooth background execution mode ,以确保您的应用继续在后台运行。

同样,在iOS 10.0或之后链接的iOS应用程序必须在其Info.plist文件中包含使用说明键,以说明需要访问的数据类型,否则它将崩溃。 InfoPlistKeyReference

您可以阅读: 核心蓝牙背景执行模式

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

如果您gets restored此处gets restored ,则意味着您restart your app ,我认为您需要重新连接,因为终止应用程序后它将失去连接。

如果您gets restored此处gets restored ,则意味着从background enter foreground ,如果启用了后台模式,因为您的应用仍在后台运行,我认为您无需重新连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM