5

我们需要从蓝牙设备获取数据到 IOS 设备。我们正在使用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。请告诉我我的代码有什么问题。

4

0 回答 0