簡體   English   中英

核心藍牙iOS 8

[英]Core bluetooth ios 8

我使用Core Bluetooth連接兩個ios設備。 一切對於ios 7來說都是完美的,但是對於ios 8則沒有。 帶有ios 8的設備作為外圍設備不可見。

一些代碼:

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


}

iOS 8.0藍牙外圍設備管理器不提供addService的回調 -該問題可以幫助我解決此問題。 只需要移動

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

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral方法:

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

}

現在,一切都適用於ios 6,7,8。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM