当我NSLog characteristic.value
时,它显示为<1100>
或<2200>
。我知道这是十六进制的。当我改变价值时,我很困惑。
非常感谢任何帮助。
目前我正在执行以下操作,但在更改值时变为null。
- (IBAction)deviceSwitchPressed:(id)sender {
if ([sender isOn]) {
[activePeripheral writeValue:[@"1100" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:switchCharacterictic type:CBCharacteristicWriteWithResponse];
} else {
[activePeripheral writeValue:[@"2200" dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:switchCharacterictic type:CBCharacteristicWriteWithResponse];
}
NSLog(@"characteristic.value = %@", switchCharacterictic.value);
}
答案 0 :(得分:1)
这是我用来将十六进制字符串值转换为数据对象的方法。
NSString *command = hexString;
command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [command length]/2; i++) {
byte_chars[0] = [command characterAtIndex:i*2];
byte_chars[1] = [command characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
但是,为特征写入值并不能保证将返回的内容。外围设备可以以任何方式处理该数据。