gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 17:51:38 27 4
gpt4 key购买 nike

我正在将体重秤设备集成到我们的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com