如何将字符串转换为 NSData 以将其传递给 BLE 设备?
Posted
技术标签:
【中文标题】如何将字符串转换为 NSData 以将其传递给 BLE 设备?【英文标题】:How to convert string into NSData to pass it to BLE Device? 【发布时间】:2017-07-28 12:07:31 【问题描述】:我有文本字段,用户输入的任何文本都不知道长度, 然后我必须将其转换为十六进制。我已经通过这段代码完成了。
NSString * str = @"Hello World";
NSString * hexStr = [NSString stringWithFormat:@"%@",
[NSData dataWithBytes:[str cStringUsingEncoding:NSUTF8StringEncoding]
length:strlen([str cStringUsingEncoding:NSUTF8StringEncoding])]];
for(NSString * toRemove in [NSArray arrayWithObjects:@"<", @">", @" ", nil])
hexStr = [hexStr stringByReplacingOccurrencesOfString:toRemove withString:@""];
NSLog(@"%@", hexStr); 48656c6c6f20776f726c64
但困难的部分从那开始。如何将每两个十六进制分成 uint8_t。因为我不知道长度,但是用户可以写任何长度的字符串,这将是巨大的十六进制。如果我知道长度我可以这样做
NSMutableData *concatenate = [NSMutableData data];
uint8_t first=0x48;
uint8_t second = 0x56;
uint8_t third = 0x6c;
uint8_t fourth = 0x6f;
uint8_t fifth=0x20;
uint8_t sixth=0x77;
uint8_t seventh=0x6f;
uint8_t eighth=0x72;
uint8_t ninth=0x6c;
uint8_t tenth=0x64;
NSData* one = [NSData dataWithBytes:(void*)&first length:sizeof(first)];
NSData* two = [NSData dataWithBytes:(void*)&second length:sizeof(second)];
NSData* three = [NSData dataWithBytes:(void*)&third length:sizeof(third)];
NSData* four = [NSData dataWithBytes:(void*)&fourth length:sizeof(fourth)];
NSData* five = [NSData dataWithBytes:(void*)&fifth length:sizeof(fifth)];
NSData* six = [NSData dataWithBytes:(void*)&sixth length:sizeof(sixth)];
NSData* seven = [NSData dataWithBytes:(void*)&seventh length:sizeof(seventh)];
NSData* eight = [NSData dataWithBytes:(void*)&eighth length:sizeof(eighth)];
NSData* nine = [NSData dataWithBytes:(void*)&nineth length:sizeof(nineth)];
NSData* ten = [NSData dataWithBytes:(void*)&tenth length:sizeof(tenth)];
[concatenate appendData:one];
[concatenate appendData:two];
[concatenate appendData:three];
[concatenate appendData:four];
[concatenate appendData:five];
[concatenate appendData:six];
[concatenate appendData:seven];
[concatenate appendData:eight];
[concatenate appendData:nine];
[concatenate appendData:ten];
但是当我不知道长度时,我不知道。任何帮助或线索将不胜感激。
【问题讨论】:
这就像有人向你要一张某物的照片,所以你拍下照片,用照片制作一幅画,然后给他们一张画的照片。您在第一步已经NSData
。
嗨乔希,在这个用户将写入命令到 ble 设备。 ios 中无法直接将 NSString 发送到 ble 设备。所以首先我必须将其转换为十六进制,然后拆分为 uint_8 并连接到 NSMutableData 以在命令中传递。
【参考方案1】:
第一次你应该将 NSString 转换为 NSData
NSString* str = @"Hello World";
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
接下来您将此数据发送到设备
[discoveredPeripheral writeValue:data forCharacteristic:charForValue type:CBCharacteristicWriteWithResponse];
您可以在此处发送带有设备响应的数据,也可以不发送。当然,您的设备应该已准备好接收此类数据(有/无响应)。
为了理解 foundPeripheral 是您从
收到的变量-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
charForValue 是接收到的变量
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
为了必要的服务和特性。
【讨论】:
以上是关于如何将字符串转换为 NSData 以将其传递给 BLE 设备?的主要内容,如果未能解决你的问题,请参考以下文章
如何最好地将 CString 转换为 BSTR 以将其作为“in”参数传递给 COM 方法?
如何实现“tzinfo”以将当前 GMT 偏移到 EST(GMT -4:00)?