如何将 <1100> 或 <2200> 写入我的 CBPeripheral 上的特征值?
How do I write <1100> or <2200> to the value of a characteristic on my CBPeripheral?
当我 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);
}
这是我用来将十六进制字符串值转换为数据对象的方法。
NSString *command = hexString;
command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'[=10=]','[=10=]','[=10=]'};
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];
}
但是,将值写入特征并不能保证返回值。外围设备可以以任何它想要的方式处理该数据。
当我 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);
}
这是我用来将十六进制字符串值转换为数据对象的方法。
NSString *command = hexString;
command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'[=10=]','[=10=]','[=10=]'};
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];
}
但是,将值写入特征并不能保证返回值。外围设备可以以任何它想要的方式处理该数据。