Corebluetooth NSMutableArray具有特征值

时间:2016-01-05 04:40:00

标签: ios objective-c arrays core-bluetooth

使用setNotify = YES属性我想创建一个数组,该数组使用以下代码(Xcode7.2)中转换的'characteristic.value'值填充。到目前为止,我只有一个空数组,所有相同的值,或一次只有一个值

我已经实施了

@property (strong, nonatomic) NSMutableArray *voltageArray; //didn't work so 
I deleted and tried the following...

我尝试使用它的方法是didUpdateCharacteristic,

 if (characteristic.isNotifying)
    {
        NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value 
            encoding:NSUTF8StringEncoding];
        NSNumber *number = [NSNumber numberWithUnsignedShort:stringFromData];
        number = @([number floatValue] *5/2034);
        NSLog(@"Characteristic Value: %@", _voltageArray);
    }

我还在didUpdateValueMethod中尝试了以下内容,以及无数其他内容,

_voltageArray=[[NSMutableArray alloc] init];
[self.voltageArray addObject:number];
 NSLog(@"Array: %@", _voltageArray);

我得到的最好的是全部相同的值或全部为null。如何将这些无符号电压值放入数组?

更新了解决方案

@property (strong, nonatomic) NSMutableArray *voltageArray;

-(void)peripheral: didUpdateValueForCharacteristic: error:{
if (characteristic.isNotifying)
    {
    if(!self.voltageArray){//Checks if voltageArray has been initialised
    self.voltageArray = [[NSMutableArray alloc] init];//If not, initialise the array
}
    // generate characteristic value
    NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
    unsigned short baseValue = (unsigned short)stringFromData;
    NSNumber *number = [NSNumber numberWithUnsignedShort:baseValue];
    number = @([number floatValue] *5/2034);
    NSLog(@"Characteristic Value: %@", number);

    // give potential recording
    self.voltageLabel.text = [NSString stringWithFormat:@"%@",number];

    // datasource points y-axis
    [_voltageArray addObject:number];
    NSLog(@"Array: %@", _voltageArray);

1 个答案:

答案 0 :(得分:0)

可能会有一些错误导致您的问题。

首先:

    NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value 
        encoding:NSUTF8StringEncoding];
    NSNumber *number = [NSNumber numberWithUnsignedShort:stringFromData];
    number = @([number floatValue] *5/2034);

第一种方法需要unsigned short类型作为输入,但是您传递的是NSString。你可以这样做:

NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value 
        encoding:NSUTF8StringEncoding];
unsigned short baseValue = (unsigned short)stringFromData.intValue;
float convertedValue = (float)baseValue *5.0/2034.0; //5.0/2034.0 -> so it typecasts the result value as a float, not int
NSNumber *number = [NSNumber numberWithFloat: convertedValue];
[self.voltageArray addObject:number];

其次,每次调用didUpdateValueForCharacteristic时,您似乎都在初始化数组。试试这个:

-(void)peripheral: didUpdateValueForCharacteristic: error:{
    if (characteristic.isNotifying)
    {
    if(!self.voltageArray){//Checks if voltageArray has been initialised
        self.voltageArray = [[NSMutableArray alloc] init];//If not, initialise the array
    }
    NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value 
        encoding:NSUTF8StringEncoding];
    unsigned short baseValue = (unsigned short)stringFromData.intValue;
    float convertedValue = (float)baseValue *5.0/2034.0;
    NSNumber *number = [NSNumber numberWithFloat: convertedValue];
    [self.voltageArray addObject:number];
    }
}

希望这有帮助。