ios - 解释从 iOS 中的蓝牙体重秤设备接收到的字节

标签 ios iphone ipad bluetooth gatt

我正在将体重秤设备集成到我们的 iOS 应用程序中。设备名称 nutriscale 体重秤。我正在使用苹果提供的 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;}

请帮助我编写这段代码。我做错了什么?

最佳答案

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

关于ios - 解释从 iOS 中的蓝牙体重秤设备接收到的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31826161/

相关文章:

php - APNS:无效的 token 导致所有后续的推送通知失败

ios - ${PRODUCT_NAME} 未定义错误

iphone - 在不改变顺序的情况下将 nsdictionary 的内容放入 nsarray

ios - iPad 上操作表的背景颜色

ios - 如何以编程方式在同一 WIFI 网络上检索设备的 mac 地址

ios - 每台计算机上缺少应用程序标识符和访问钥匙串(keychain)组

iphone - 在公共(public) UIView 中重新加载数据

iphone - 如何为 UIImage 应用 Warping 技术?

iPad 可滑动面板控制,如推特

iphone - 是否有 Safari Mobile(即 iPad 和 iPhone)支持的字体列表?