CoreBluetooth连接后立即断开

6

我需要使用Corebluetooth在iPhone和iPad之间交换信息。

我的iPhone作为中央设备,iPad作为外围设备。

我正在广播我的服务,但当我在中央设备上进行以下操作时:

peripheral:didDiscoverServices:

我在这个方法中获取的外围设备服务是空的。

几秒钟后,我会因为这个错误从外围设备断开连接:

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:didConnectPeripheral: 中,我没有使用正确的服务UUID调用 [peripheral discoverServices:]。感谢您的帮助 :)。


你能展示一些你的代码吗? - Yazid
会看一下。顺便问一句,你尝试过对设备进行电源循环吗?这可能不能解决断开连接的问题,但有时空服务列表为空可能与蓝牙缓存有关。 - Yazid
1
你能否也发布一下你的其他CBCentralManager方法(didDiscoverPeripheraldidConnectPeripheral等)?另外,对于你的CBPeripheralManager,最好的做法是在确认设置服务后才调用startAdvertising。你应该实现didAddService委托方法并在其中执行。 - Yazid
将didConnect中的代码添加进去。 - Tommy Devoy
将问题标记为已解决。 - Tommy Devoy
显示剩余2条评论
1个回答

3

我找到了问题所在,在 centralManager:didConnectPeripheral: 中,我没有使用正确的服务UUID来调用 [peripheral discoverServices:]。谢谢你的帮助 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接