解释从iOS

时间:2015-08-05 07:35:04

标签: ios iphone ipad bluetooth gatt

我正在我们的iOS应用程序中集成一个体重秤设备。设备名称营养品重量标度。我正在使用Apple提供的API -CBCentralManager来连接和获取体重秤的数据。我能够检测到蓝牙设备的服务和特性,并在连接后从体重秤设备获取一些数据,但无法解释该数据。如果它低于255克我能够增加体重。如果它超过255.它给了我体重255的答案。     请为此纠正我。 这是我的代码:

  When i call  [aPeripheral readValueForCharacteristic:aChar]; A delegate method below is being called.

     - (void) peripheral:(CBPeripheral *)aPeripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {// NSLog(@"Descriptor %@",[characteristic properties]);if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:HELLOBLUETOOTH_CHARACTERISTICS_NAME_UUID]])
{
    pName = [[NSString alloc] initWithUTF8String:[[characteristic value]bytes]];
    NSError *errorVa;
    NSLog(@"KeyfobViewController didUpdateValueForCharacteristic %@", characteristic);
    [aPeripheral setNotifyValue:YES forCharacteristic:characteristic];
    [self getWeightData:characteristic error:errorVa];
    }}

解释我写这个方法的字节

 (void) getWeightData:(CBCharacteristic *)characteristic error:(NSError *)error{
// Get the Heart Rate Monitor BPM
NSData *data = [characteristic value];// 1

const uint8_t *reportData = [data bytes];
const uint16_t *reportData1 = [data bytes];
uint16_t weightValue = 0;
uint16_t weightValue1 = 0;

if(reportData)
{
    if ((reportData[0] & 0x01) == 0) {          // 2
        // Retrieve the weight from the scale
        weightValue = reportData[1];
        int result= CFSwapInt16LittleToHost(*(uint16_t *)(&reportData[1]));

    }
    else
    {
        weightValue = CFSwapInt32LittleToHost(*(uint32_t *)(&reportData[1]));  // 3
        int result= CFSwapInt32LittleToHost(*(uint32_t *)(&reportData[1]));
        NSLog(@"weightValue1 - %hhu",weightValue);

    }

    NSMutableArray *arrr = [NSMutableArray new];

    uint8_t byte1 = reportData[0];
    for (int i = 0; i < 8; i++) {int mask = 1 << i;if ((byte1 & mask) == 0) {[arrr addObject:@"0"];} else {[arrr addObject:@"1"];}}
    NSLog(@"values1 - %@%@%@%@%@%@%@%@",arrr[7],arrr[6],arrr[5],arrr[4],arrr[3],arrr[2],arrr[1],arrr[0]);

    [arrr removeAllObjects];

    for (int i = 0; i < 16; i++) {int mask = 1 << i;if ((weightValue1 & mask) == 0) {[arrr addObject:@"0"];} else {[arrr addObject:@"1"];}}

    NSLog(@"values2 - %@%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@",arrr[15],arrr[14],arrr[13],arrr[12],arrr[11],arrr[10],arrr[9],arrr[8],arrr[7],arrr[6],arrr[5],arrr[4],arrr[3],arrr[2],arrr[1],arrr[0]);


    //        NSLog(@"values0 - %@%@%@%@%@%@%@%@",arrr[0],arrr[1],arrr[2],arrr[3],arrr[4],arrr[5],arrr[6],arrr[7]);

    //        NSLog(@"values2 - %@%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@",arrr[0],arrr[1],arrr[2],arrr[3],arrr[4],arrr[5],arrr[6],arrr[7],arrr[8],arrr[9],arrr[10],arrr[11],arrr[12],arrr[13],arrr[14],arrr[15]);

}
// Display the weight value to the UI if no error occurred
if( (characteristic.value)  || !error )
{   //

    NSString *weight = [NSString stringWithFormat:@"%i", weightValue];
    if([weight floatValue])
    {
        NSUserDefaults *defaultObject =  [NSUserDefaults standardUserDefaults];
        [defaultObject setObject:data forKey:@"data"];
        [defaultObject synchronize];
        NSString *strWeight=@"";
        strWeight = [NSString stringWithFormat:@"%@",weight];
        strWeight = [NSString stringWithFormat:@"%.1f",[strWeight floatValue]*0.035274];//
        //[self bluetoothResponseToClass];
    }
}
return;}

请在此代码中帮助我。我做错了什么?

1 个答案:

答案 0 :(得分:-1)

Replace your function with below function


-(void) getWeightData:(CBCharacteristic *)characteristic error:(NSError *)error
{
    // Get the Heart Rate Monitor BPM
    NSData *data = [characteristic value];// 1
    const uint8_t *reportData = [data bytes];
    uint16_t weightValue = 0;
    uint16_t chkValue = 0;
    if(reportData)
    {
        chkValue = reportData[0];
         weightValue = CFSwapInt32LittleToHost(*(uint32_t *)(&reportData[1]));
        int var = (chkValue % 160);
        weightValue =  weightValue + var * 256;


        NSMutableArray *arrr = [NSMutableArray new];

        uint8_t byte1 = reportData[0];
        for (int i = 0; i < 8; i++) {int mask = 1 << i;if ((byte1 & mask) == 0) {[arrr addObject:@"0"];} else {[arrr addObject:@"1"];}}
        NSLog(@"values1 - %@%@%@%@%@%@%@%@",arrr[7],arrr[6],arrr[5],arrr[4],arrr[3],arrr[2],arrr[1],arrr[0]);

        [arrr removeAllObjects];

        for (int i = 0; i < 16; i++) {int mask = 1 << i;if ((chkValue & mask) == 0) {[arrr addObject:@"0"];} else {[arrr addObject:@"1"];}}

        NSLog(@"values2 - %@%@%@%@%@%@%@%@%@%@%@%@%@%@%@%@",arrr[15],arrr[14],arrr[13],arrr[12],arrr[11],arrr[10],arrr[9],arrr[8],arrr[7],arrr[6],arrr[5],arrr[4],arrr[3],arrr[2],arrr[1],arrr[0]);


    }
    // Display the weight value to the UI if no error occurred
    if( (characteristic.value)  || !error )
    {   //

        NSString *weight = [NSString stringWithFormat:@"%i", weightValue];

        lbl_Weight.text = [NSString stringWithFormat:@"%hu", weightValue];
        if([weight floatValue])
        {
            NSUserDefaults *defaultObject =  [NSUserDefaults standardUserDefaults];
            [defaultObject setObject:data forKey:@"data"];
            [defaultObject synchronize];
            NSString *strWeight=@"";
            strWeight = [NSString stringWithFormat:@"%@",weight];
            strWeight = [NSString stringWithFormat:@"%.1f",[strWeight floatValue]*0.035274];//
            //[self bluetoothResponseToClass];
        }
    }
    return;
}