0

当我NSLog characteristic.value显示为<1100>or时<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);

}
4

1 回答 1

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

但是,将值写入特征并不能保证将返回该值。外围设备可以以它想要的任何方式处理该数据。

于 2015-11-17T16:31:31.307 回答