我们需要从IOS设备获取BluetooothDevices的数据。我们正在使用core_bluetooth.frameworks
。 didDisconnectPeripheral在didConnectPeripheral之后调用。我们得到的错误是
错误是Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo={NSLocalizedDescription=The specified device has disconnected from us.}
我们尝试的代码是: //在ViewDidLoad中
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerOptionShowPowerAlertKey, nil];
self.central=[[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];
//self.central=[[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)];
self.discoveredPeripherals=[NSMutableArray new];
//开启/关闭蓝牙
-(void) centralManagerDidUpdateState:(CBCentralManager *)central {
NSString *stateString = nil;
switch(central.state)
{
case CBCentralManagerStateResetting:
stateString = @"The connection with the system service was momentarily lost, update imminent.";
break;
case CBCentralManagerStateUnsupported:
stateString = @"The platform doesn't support Bluetooth Low Energy.";
break;
case CBCentralManagerStateUnauthorized:
stateString = @"The app is not authorized to use Bluetooth Low Energy.";
break;
case CBCentralManagerStatePoweredOff:
stateString = @"Bluetooth is currently powered off.";
break;
case CBCentralManagerStatePoweredOn:
stateString = @"Bluetooth is currently powered on and available to use.";
}
}
//查询
NSLog(@"Discovered peripheral %@ (%@)",peripheral.name,peripheral.identifier.UUIDString);
if (![self.discoveredPeripherals containsObject:peripheral] ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.discoveredPeripherals addObject:peripheral];
[self.tableview insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:self.discoveredPeripherals.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];
});
}
// didConnectPeripheral
[self.activePeripheral discoverServices:@[[CBUUID UUIDWithString:@"00001c00-d102-11e1-9b23-00025b00a5a5"]]];
// didDisconnectPeripheral
NSLog(@"error is %@",error.description);
我们不明白为什么它会在didConnectPeripheral之后调用didDisconnection。请告诉我我的代码有什么问题。