【发布时间】:2015-02-27 20:15:31
【问题描述】:
我正在通过核心蓝牙 (BLE) 从硬件设备读取数据。我正在阅读的特征之一是将结构压缩为单个值。编程到板上的结构如下所示:
typedef struct
{
uint8 id;
uint32 dur;
uint16 dis;
} record;
我正在解析的大多数其他特征都是单一类型,uint8、uint32,等等。
如何遍历字节并将每个单独的特征解析为原生类型或NSString?有没有办法遍历 NSData 对象的字节或子串?
NSData *data = [characteristic value]; // characteristic is of type CBCharacteristic
NSUInteger len = data.length;
Byte *bytes = (Byte *)[data bytes];
for (Byte in bytes) { // no fast enumeration here, but the general intention is to iterate byte by byte
// TODO: parse out uint8
// TODO: parse out uint32
// TODO: parse out uint16
}
【问题讨论】:
-
如果结构始终是固定布局,那么只需直接访问字节 - [data bytes][0] 将是您的 id。 [data bytes[1]]-[data bytes[4] will be dur and bytes 5 & 6 will be dis
标签: ios core-bluetooth