Core bluetooth ios 8

[亡魂溺海] 提交于 2019-12-23 05:13:55

问题


I use Core Bluetooth to connect two ios devices. Everything is working perfect for ios 7, but for ios 8 no. Device with ios 8 is not visible as peripheral device.

Some code:

    _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];

        [_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}];

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

    if (error) {
        [self cleanup];
        return;
    }

    for (CBService *service in peripheral.services) {
        [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service];

    }

}

- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{


    if(error){
        [self cleanup];
        return;
    }

    for (CBCharacteristic *charactristic in service.characteristics) {

        if ([charactristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
            [peripheral setNotifyValue:YES forCharacteristic:charactristic];
        }
    }

}

- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{

    if (error) {
        return;
    }

    NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];

    [peripheral setNotifyValue:NO forCharacteristic:characteristic];
    [_centralManager cancelPeripheralConnection:peripheral];

    [_data appendData:characteristic.value];

    NSDictionary *recievedData = [NSDictionary dictionaryWithObject:stringFromData forKey:@"receivedData"];

    [[NSNotificationCenter defaultCenter]
     postNotificationName:DATA_FROM_PERIPHERAL_RECEIVED
     object:self userInfo:recievedData];


    //   }

}

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{

    if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
        return;
    }

    if (characteristic.isNotifying) {
        NSLog(@"Notification began on %@", characteristic);
    }else{
        [_centralManager cancelPeripheralConnection:peripheral];
    }


}

回答1:


iOS 8.0 bluetooth peripheral manager giving no callback for addService - This question helps me to fix this issue. Just need to move

 [_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}];

to -(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral method:

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{

    if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
        return;
    }

    if (peripheral.state == CBPeripheralManagerStatePoweredOn) {

        [_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey: @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]}];

        self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
        CBMutableService *transferService = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID] primary:YES];
        transferService.characteristics = @[_transferCharacteristic];
        [_peripheralManager addService:transferService];
    }

}

Now everything works great for ios 6,7,8.



来源:https://stackoverflow.com/questions/26120834/core-bluetooth-ios-8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!