How do I write <1100> or <2200> to the value of a characteristic on my CBPeripheral?

我的未来我决定 提交于 2020-01-17 05:04:24

问题


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.

Any help would be much appreciated.

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);

}

回答1:


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.



来源:https://stackoverflow.com/questions/33761171/how-do-i-write-1100-or-2200-to-the-value-of-a-characteristic-on-my-cbperiphe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!