Ios 连接后立即断开蓝牙连接

Ios 连接后立即断开蓝牙连接,ios,iphone,ipad,ios7,core-bluetooth,Ios,Iphone,Ipad,Ios7,Core Bluetooth,我实际上是在iPhone和iPad之间使用蓝牙技术交换信息 我的iPhone是中央设备,iPad是外围设备 我正在为我的服务做广告,但在我的中央电视台上,当我通过: peripheral:didDiscoverServices: 我在该方法中获得的peripheral.services是空的 在我用这个错误与外设断开连接几秒钟后: DISCONNECT-ERROR desc : Error Domain=CBErrorDomain Code=7 "The specified device ha

我实际上是在iPhone和iPad之间使用蓝牙技术交换信息

我的iPhone是中央设备,iPad是外围设备

我正在为我的服务做广告,但在我的中央电视台上,当我通过:

peripheral:didDiscoverServices:
我在该方法中获得的peripheral.services是空的

在我用这个错误与外设断开连接几秒钟后:

DISCONNECT-ERROR desc : Error Domain=CBErrorDomain Code=7 "The specified device has disconnected from us." UserInfo=0x16e60f90 {NSLocalizedDescription=The specified device has disconnected from us.}
我不知道发生了什么事

编辑:

在中央,我有:

-(void)startScanning{
    [super startScanning];
    // Scan for devices
    [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:PERIPHERAL_SERVICE_UUID]] options:nil];
}

#pragma mark Peripheral Delegate

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
    if (error) {
        [self cleanup];
        return;
    }

    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:NEW_COMMANDS_NOTIFIER_CHARACTERISTICS_UUID]] forService:service];
    }
    // Discover other characteristics
}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
    if (error) {
        [self cleanup];
        return;
    }

    for (CBCharacteristic *characteristic in service.characteristics) {
        if (self.commandsFromIpadCharacteristic != characteristic) {
            self.commandsFromIpadCharacteristic = characteristic;
        }
        if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:NEW_COMMANDS_NOTIFIER_CHARACTERISTICS_UUID]]) {
            [peripheral setNotifyValue:YES forCharacteristic:characteristic];

        }
    }
}
在外围方面,我有:

#pragma mark CBPeripheralManagerDelegate

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
    if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
        return;
    }

    if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
        self.datasFromIphoneCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:NEW_DATAS_FROM_IPHONE_CHARACTERISTICS_UUID] properties:CBCharacteristicPropertyWrite value:nil permissions:CBAttributePermissionsWriteable];
        self.commandNotifierCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:NEW_COMMANDS_NOTIFIER_CHARACTERISTICS_UUID] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

        CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:PERIPHERAL_SERVICE_UUID] primary:YES];

        transferService.characteristics = @[self.datasFromIphoneCharacteristic, self.commandNotifierCharacteristic];

        [self.peripheralManager addService:transferService];

        [self.peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:PERIPHERAL_SERVICE_UUID]]}];
    }
}


- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error {

    if (error) {
        NSLog(@"Error advertising: %@", [error localizedDescription]);
    }
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
    if (characteristic == self.commandNotifierCharacteristic){
        // on envoie le message au delegate
        if([[self delegate] respondsToSelector:@selector(iPhoneIsConnectedToIpad:)]) {
            [[self delegate] iPhoneIsConnectedToIpad:YES];
        }
    }
}

-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic{
    if (characteristic == self.commandNotifierCharacteristic){
        // on envoie le message au delegate
        if([[self delegate] respondsToSelector:@selector(iPhoneIsConnectedToIpad:)]) {
            [[self delegate] iPhoneIsConnectedToIpad:NO];
        }
    }
}
编辑答案:


我在
centralManager:didconnectperipal:
中发现了问题,我没有使用
[peripheral discoverServices:
调用正确的服务UUID。谢谢您的帮助:)。

我在
centralManager:DidConnectPeripal:
中发现了问题,我没有使用
[peripheral DiscoversServices:
调用正确的服务UUID。谢谢你的帮助:)。

你能展示一下你的代码吗?我刚刚添加了一些代码。我来看看。顺便说一句,你有没有试过让设备断电?这可能无法解决断开连接的问题,但有时空服务列表可能与蓝牙缓存有关。您是否也可以发布其他
CBCentralManager
方法(
didiscoverperipal
diconnectperipal
等)?此外,对于您的
cbperipheraldmanager
,最好的做法是在确认您的服务已设置完毕后才调用
startAdvertising
。您应该实现
didAddService
delegate方法并在那里执行。从didConnect添加代码