HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Corebluetooth在连接后断开连接大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@
我实际上是使用COrebluetooth在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中:didConnectPeripheral:我没有使用[peripheral discoverservices:]调用正确的服务UUID.谢谢您的帮助 :).

解决方法

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

大佬总结

以上是大佬教程为你收集整理的ios – Corebluetooth在连接后断开连接全部内容,希望文章能够帮你解决ios – Corebluetooth在连接后断开连接所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。