iOS 8:后台应用程序CBCentralManager委托方法未调用DidDiscoveryPeripheral、didConnectPeripheral、DidFailToConnectionPeripheral时

iOS 8:后台应用程序CBCentralManager委托方法未调用DidDiscoveryPeripheral、didConnectPeripheral、DidFailToConnectionPeripheral时,ios,ios8,background-process,core-bluetooth,Ios,Ios8,Background Process,Core Bluetooth,我在iOS 8上使用Xcode 6。当应用程序在后台运行时,我正在尝试搜索并连接蓝牙设备。我正在使用corebluetooth框架。我使用Xcode功能选项添加了蓝牙中心和蓝牙外围设备 代码: Appdelegate.h @interface WSAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate,CBCentralManagerDelegate,CBPeripheralDelegate&

我在iOS 8上使用Xcode 6。当应用程序在后台运行时,我正在尝试搜索并连接蓝牙设备。我正在使用corebluetooth框架。我使用Xcode功能选项添加了蓝牙中心和蓝牙外围设备

代码:

Appdelegate.h

@interface WSAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate,CBCentralManagerDelegate,CBPeripheralDelegate>

@property (strong, nonatomic) NSString *savedUUID;
@property (strong, nonatomic) CBCentralManager *CBCM;
未调用DidDiscoveryPeripheral方法,因此我使用上次连接的设备的保存uuid,但它未连接,也未调用委托方法didConnectPeripheral和DidFailToConnectionPeripheral。
那么,当应用程序在后台运行时,如何解决这个问题并与蓝牙连接呢?

我不会使用BackgroundTask在后台扫描外围设备

1) 创建CBCentralManager时,请确保为CBCentralManager恢复标识符键指定了一个选项

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

2) 在AppDelegate的didFinishLaunchingWithOptions方法的launchOptions中查找UIApplicationLaunchOptions BluetoothCentralsKey:

NSArray *centralManagerIdentifiers =
    launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];
这用于恢复您的中央管理器。(与1中的密钥名称相同)


3) 确保在CBCentralManagerDelegate中实施willRestoreState。请参阅上面列出的文档。

是否设置了断点以确认RetrievePeripheralsWithinIdentifiers返回CBPeripheral?是的,我已经这样做了,并且还打印了值“array”(“”),但它仍然不起作用。这是外围设备广告吗?此外,您不应在
beginBackgroundTask
块中初始化CBManager。你应该直接初始化它不,外设不是广告。让我试试。我已经试过了,它显示“CBPeripherManagerStatePoweredOn”是正确的,但仍然不起作用。
NSArray *centralManagerIdentifiers =
    launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];