2014-09-30 85 views
0

我使用核心蓝牙来连接两个ios设备。一切工作完美的ios 7,但对于ios 8没有。带有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]; 
    } 


} 
+0

请让你的问题更具体与你的尝试,你怀疑和类似的信息。 – allprog 2014-10-01 15:08:38

+0

使用LigthBlue,其他设备是否可以找到它? – Larme 2014-10-02 08:48:23

+0

LightBlue未发现ios 8上的设备。与我的应用程序相同的情况。 – Natali 2014-10-02 09:09:33

回答

0

iOS 8.0 bluetooth peripheral manager giving no callback for 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的伟大工程。