我有一个实现 coreBluetooth 框架以连接蓝牙钥匙串的目标 c 应用程序。一切都是正确的,我用蓝牙钥匙串连接,如果我去后台我的设备保持连接。如果我的设备断电或丢失,则重新连接。我的应用程序会检测我何时单击带有服务和 didUpdateValueForCharacteristic 的按钮蓝牙钥匙串。所以这一切都是正确的。
但是我正在尝试将恢复属性与 willRestoreState 函数一起使用,但这不起作用......我将我的应用程序发送到后台,但是当我重新启动我的 iPhone 时,这不会重新连接并且不起作用 didUpdateValueForCharacteristic。
我已将 centralManager 和所有功能蓝牙委托添加到 AppDelegate 以及恢复应用程序的功能,并且我使用自定义队列来连接 centralManager 蓝牙。但我不知道是什么问题,为什么不重新连接然后重新启动。
@interface AppDelegate ()
@property (strong,nonatomic) CBCentralManager *central;
@property (copy,nonatomic) NSString *targetPeripheral;
@property (strong,nonatomic) NSMutableArray *discoveredPeripherals;
@property (strong,nonatomic) CBPeripheral *connectedPeripheral;
@property (strong,nonatomic) CBUUID *deviceInfoUUID;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
ViewController *myloginController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"viewController"];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:myloginController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
self.discoveredPeripherals=[NSMutableArray new];
self.deviceInfoUUID=[CBUUID UUIDWithString:@"FFE0"];
//I have added this code to connect then reboot iPhone:
NSArray *centralManagerIdentifiers = launchOptions[UIApplicationLaunchOptionsBluetoothCentralsKey];
if ((centralManagerIdentifiers) && (centralManagerIdentifiers.count > 0)) {
// The system knows about one or more centrals that need to be restored.
for (NSString *identifier in centralManagerIdentifiers) {
dispatch_queue_t queue = dispatch_queue_create("com.myco.cm", DISPATCH_QUEUE_SERIAL);
self.central = [[CBCentralManager alloc] initWithDelegate:self queue:queue options:@{CBPeripheralManagerOptionRestoreIdentifierKey : identifier,CBCentralManagerOptionShowPowerAlertKey:@YES}];
[self.referencesToCentrals addObject:self.central];
}
}
else {
dispatch_queue_t queue = dispatch_queue_create("com.myco.cm", DISPATCH_QUEUE_SERIAL);
self.central = [[CBCentralManager alloc] initWithDelegate:self queue:queue options:@{CBPeripheralManagerOptionRestoreIdentifierKey : @"1C7757658-D187-99D2-9578-7B0976AAAEB4",CBCentralManagerOptionShowPowerAlertKey:@YES }];
[self.referencesToCentrals addObject:self.central];
}
//An this is my willrestoreState function
-(void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict {
dispatch_async(dispatch_get_main_queue(), ^() {
NSArray *peripherals = dict[CBCentralManagerRestoredStatePeripheralsKey];
if ([peripherals count]>0) { // There are peripherals
for (CBPeripheral* p in peripherals) {
if ([p.identifier isEqual:[CBUUID UUIDWithString:self.UUID]]) { // wich are sensorTags
[self.central connectPeripheral:[peripherals firstObject] options:nil]; // so let's reconnect
}
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error {
NSString *content = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSLog(@"Receive from Peripheral: %@",content);
}
-(void) startScan {
[self.central scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FFE0"]] options:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"We are in background");
}
-(void) centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBCentralManagerStatePoweredOn:
[self startScan];
break;
case CBManagerStateUnknown:
break;
case CBManagerStatePoweredOff:
break;
case CBManagerStateResetting:
break;
case CBManagerStateUnsupported:
break;
case CBManagerStateUnauthorized:
break;
}
}
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
if([self.central retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:@"FFE0"]]] != 0){
dispatch_async(dispatch_get_main_queue(), ^{
self.nameBT = peripheral.name;
self.UUID = peripheral.identifier.UUIDString;
});
}
if (![self.discoveredPeripherals containsObject:peripheral]){
dispatch_async(dispatch_get_main_queue(), ^{
[self.discoveredPeripherals addObject:peripheral];
});
}
if (self.connectedPeripheral) {
[self.central cancelPeripheralConnection:self.connectedPeripheral];
}
self.targetPeripheral=peripheral.identifier.UUIDString;
[self.central connectPeripheral:peripheral options:nil];
}
-(void) centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
self.connectedPeripheral=peripheral;
dispatch_async(dispatch_get_main_queue(), ^{
self.nameBT = peripheral.name;
self.UUID = peripheral.identifier.UUIDString;
});
peripheral.delegate=self;
[peripheral discoverServices:@[self.deviceInfoUUID]];
}
And I have added this to try if application reconnect bluetooth:
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder {
return YES;
}
-(void) centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"Disconnected from peripheral");
if ([self.targetPeripheral isEqualToString:peripheral.identifier.UUIDString]) {
NSLog(@"Retrying");
[self.central connectPeripheral:peripheral options:nil];
}
}
}
当 iPhone 自动重启而不需要打开应用程序时,如何重新连接?我究竟做错了什么?