具有特征值的 Corebluetooth NSMutableArray

Corebluetooth NSMutableArray with characteristic values

使用 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);

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

更新解决方案

@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);

有几个错误可能会导致您的问题。

第一个:

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

希望对您有所帮助。