无法在didUpdateNotificationStateForCharacteristic中获取CBcharacteristics值

时间:2015-04-15 12:55:53

标签: ios bluetooth-lowenergy core-bluetooth cbcentralmanager cbperipheral

我有一台成功连接的BLE设备。在这种情况下,每当我向BLE设备发送字符串“GET DATA”时,设备将通过向我发送某个响应来做出响应。我正在使用didDiscoverCharacteristicsForService来写入值(“获取数据”)。我期待didUpdateNotificationStateForCharacteristic中的数据,但我每次都会得到响应。

 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
    if ([service.UUID isEqual:[CBUUID UUIDWithString:@"Some service UUID"]])
    {         
         for (CBCharacteristic *aChar in service.characteristics)
         {
             if ([aChar.UUID isEqual:[CBUUID UUIDWithString:@"Some UUID"]]) 
             {
                 NSString *str = @"GET DATA";
                 NSData *someData = [str dataUsingEncoding:NSUTF8StringEncoding];
                [self.RN2058Peripheral setNotifyValue:YES forCharacteristic:aChar];
                [self.RN2058Peripheral writeValue:someData forCharacteristic:aChar type:CBCharacteristicWriteWithResponse];
             }  
         }
     }
}



- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"Some UUID"]])
    {
        NSString *value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
        NSLog(@"Value %@",value);

        NSData *data = characteristic.value;
        NSString *stringFromData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"Data ====== %@", stringFromData);
    }
}

1 个答案:

答案 0 :(得分:3)

调用

didUpdateNotificationStateForCharacteristic以响应您对setNotifyValue的调用。

当在外围设备上更改特征值时,您将调用didUpdateValueForCharacteristic委托方法。在此方法中,您可以访问特征的值。