简体   繁体   English

Corebluetooth在连接后立即断开连接

[英]Corebluetooth disconnecting just after the connection

I'm actually to exchange informations between an iPhone and an iPad using Corebluetooth. 我实际上是使用Corebluetooth在iPhone和iPad之间交换信息。

My iPhone act as the Central and my iPad as the Peripheral. 我的iPhone充当中央,我的iPad充当外围设备。

I'm advertizing my service but on my central when i'm going through the : 我正在宣传我的服务但在我的中心时,我正在通过:

peripheral:didDiscoverServices:

the peripheral.services that I get in that method is empty. 我在该方法中获得的peripheral.services是空的。

and some seconds after i'm disconnecting from the peripheral with this error : 我错误地从外设断开后几秒钟:

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.}

I don't know what is going on. 我不知道发生了什么事。

EDIT: 编辑:

on the Central side I have this : 在中央方面我有这个:

-(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];

        }
    }
}

On the Peripheral side I have : 在外围方面,我有:

#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];
        }
    }
}

EDIT ANSWER : 编辑答案:

I found the problem, In the centralManager:didConnectPeripheral: I wasn't calling the right service UUID with [peripheral discoverServices:] . 我发现了问题,在centralManager:didConnectPeripheral:我没有使用[peripheral discoverServices:]调用正确的服务UUID。 Thank you for your help :). 谢谢您的帮助 :)。

I found the problem, In the centralManager:didConnectPeripheral: I wasn't calling the right service UUID with [peripheral discoverServices:] . 我发现了问题,在centralManager:didConnectPeripheral:我没有使用[peripheral discoverServices:]调用正确的服务UUID。 Thank you for your help :). 谢谢您的帮助 :)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM