gpt4 book ai didi

iOS 8 : When App in Background CBCentralManager Delegate Method didDiscoverPeripheral, didConnectPeripheral、didFailToConnectPeripheral 未调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:47 26 4
gpt4 key购买 nike

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

代码:

Appdelegate.h

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

@property (strong, nonatomic) NSString *savedUUID;
@property (strong, nonatomic) CBCentralManager *CBCM;

AppDelegate.m

- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

NSUserDefaults *defaults=[[NSUserDefaults alloc] init];
self.savedUUID= [defaults stringForKey:@"uuid"];

UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask = 0;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{

NSLog(@"Object Created::::");
self.CBCM = [[CBCentralManager alloc] initWithDelegate:self queue:nil];


[app endBackgroundTask:bgTask];
}];

}


- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
printf("Status of CoreBluetooth central manager changed \n");

if(self.CBCM.state == CBCentralManagerStatePoweredOn)
{
//okay your good to go and can now scan
// NSLog(@"your good to go and can now scan");
NSLog(@"Searching for Device::::");
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:self.savedUUID];//where savedUUID is the string version of the NSUUID you've saved somewhere

NSArray *peripherals = [self.CBCM retrievePeripheralsWithIdentifiers:@[uuid]];

for(CBPeripheral *periph in peripherals)
{
[self.CBCM connectPeripheral:periph options:nil];
}
}
else
{
//Unable to use CentralManager methods so print out the central.state and find out why
NSLog(@"Unable to use CentralManager methods so print out the central state and find out why:::%d",self.CBCM.state);
}


}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

NSLog(@"Peripheral Found::::%@",peripheral);

}

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {

printf("Connection to peripheral with UUID successfull\r\n");

}

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
printf("Connection to peripheral with UUID Fail,didFailToConnectPeripheral\r\n");
printf("Error code was %s\r\n",[[error description] cStringUsingEncoding:NSStringEncodingConversionAllowLossy]);

}

didDiscoverPeripheral 方法未被调用,因此我使用的是上次连接设备的已保存 uuid,但它未连接且未调用委托(delegate)方法 didConnectPeripheral 和 didFailToConnectPeripheral。那么如何解决这个问题,在后台运行的时候连接蓝牙呢?

最佳答案

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

1) 创建 CBCentralManager 时,请确保您为 CBCentralManagerOptionRestoreIdentifierKey 指定一个选项。

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

参见:

https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW6

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

NSArray *centralManagerIdentifiers =
launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];

这用于恢复您的中央管理器。 (将与 1 中的 key 同名)。

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

关于iOS 8 : When App in Background CBCentralManager Delegate Method didDiscoverPeripheral, didConnectPeripheral、didFailToConnectPeripheral 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26731627/

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