我怎么写< 1100>或< 2200>到我的CBPeripheral上的特征值? [英] How do I write <1100> or <2200> to the value of a characteristic on my CBPeripheral?
本文介绍了我怎么写< 1100>或< 2200>到我的CBPeripheral上的特征值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我 NSLog特征。值
时,它显示为< 1100>
或 < 2200>
。我知道这是一个十六进制。当我更改值时,我很困惑要写什么。
When I NSLog characteristic.value
it shows as either <1100>
or <2200>
. I know this is a hexadecimal. I'm confused as what to write when I'm changing the value.
任何帮助将不胜感激。
目前,我正在执行以下操作,但更改值时为null。
At the moment I'm doing the following, but getting null when I change the values.
- (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);
}
推荐答案
我用来将十六进制字符串值转换为数据对象的内容。
This is what I use to convert hex string values into data objects.
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];
}
但是向特征中写入值并不能保证回。外设可以根据需要随时处理该数据。
However writing a value to a characteristic doesn't guarantee that is what will be returned. The peripheral can handle that data in anyway that it wants.
这篇关于我怎么写< 1100>或< 2200>到我的CBPeripheral上的特征值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文