将Characterstic.Value解析为字节整数格式 [英] Parse Characterstic.Value to byte integer format

查看:121
本文介绍了将Characterstic.Value解析为字节整数格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做核心蓝牙应用程序。我能够连接外围设备,并从中读取,写入数据。



我需要通过 characteristic.value解析我所接收的数据为整数格式。
我的特征值为< 011f6d00 00011100 00000000 04050701 05000569 07df0203 020b0d21 02ff33>



我根据理解划分了数据。
请帮助我提供用于转换数据的示例代码。由于我对iOS新手观察到很多链接,但没有找到确切答案。

 < 011f6d00 

11 00事件ID // 2字节

00事件类型// 1字节

00包数// 1字节

00 00记录计数// 2字节

04 05总持续时间// 2字节

07灵敏度// 1字节

01记录灵敏度// 1字节

05期望占用持续时间// 1字节

00未使用参数// 1字节

05预计记录持续时间// 1字节

69未使用参数// 1字节

07暂停持续时间// 1字节

df禁用帮助按钮// 1字节

02 03年// 2字节

02月份日期// 1字节

0b星期几// 1字节

0d小时// 1字节

21分钟// 1字节

02秒// 1字节

ff33> crc // 2字节


解决方案

  NSData * initialData = [yourCharacteristic value]; 

解析数据的方法是使用 subdataWithRange:











  NSData * startOfFrameData = [data subdataWithRange:NSMakeRange(0,4)]; 
NSLog(@StartOfFrameData:%@,startOfFrameData);
NSData * eventIDData = [data subdataWithRange:NSMakeRange(4,2)];
NSLog(@eventIDData:%@,eventIDData);



输出: p>

 > StartOfFrame:< 011f6d00> 
> eventIDData:< 0001>

请注意,我可能会颠倒 eventIDData 哪个范围可以是(6,2)(而不是(4,2)),然后,你必须理解数据的含义并找到正确的格式,例如(可能的) eventIDData

  UInt16 eventID; 
[eventIDData getBytes:& eventID length:sizeof(eventID)];
NSLog(@eventID:%d,eventID);

等等...

如果你想玩它而不必一次又一次地读取特征值(这意味着也连接等),这里是一种你可以使用的方法:

< (NSData *)dataWithStringHex:(NSString *)string
{
NSString * cleanString;
cleanString = [string stringByReplacingOccurrencesOfString:@< withString:@ ];
cleanString = [cleanString stringByReplacingOccurrencesOfString:@> withString:@ ];
cleanString = [cleanString stringByReplacingOccurrencesOfString:@withString:@];

NSInteger length = [cleanString length];
uint8_t buffer [length / 2];
for(NSInteger i = 0; i< length; i + = 2)
{
unsigned result = 0;
NSScanner * scanner = [NSScanner scannerWithString:[cleanString substringWithRange:NSMakeRange(i,2)]];
[scanner scanHexInt:& result];
buffer [i / 2] =结果;
}
return [[NSMutableData alloc] initWithBytes:& buffer length:length / 2];

$ / code>

使用示例:

  NSData * initialData = [self dataWithStringHex:@< 011f6d00 00011100 00000000 04050701 05000569 07df0203 020b0d21 02ff33>]; 

这样,您可以尝试解析其他示例/项目/ beta测试代码中的数据。 / p>

I am doing the Core Bluetooth application. I am able to connect the peripheral and read, write value from it.

I need to parse the data which I am receiving through characteristic.value to integer format. I had the characteristic value as <011f6d00 00011100 00000000 04050701 05000569 07df0203 020b0d21 02ff33>.

I have divided the data as per understanding. Please help me with the sample code for converting the data. As I am new to iOS observed many links but did not find the exact answer

  <011f6d00 

11 00  event id       //2 bytes

00    event type      //1 byte

00    No of packets            //1 byte

00 00 record count               //2 byte

04 05 total duration            //2 byte

07       sensitivity            //1 byte

01    recording sensitivity   //1 byte

05    expected seizure duration  //1 byte

00    Not used parameter       //1 byte

05    Expected recorded duration     //1 byte

69    not used parameters     //1 byte

07    snooze duration       //1 byte

df    disable watch help button    //1 byte   

02 03  year        //2 byte

02    date of month     //1 byte

0b    day of week      //1 byte

0d    hour             //1 byte

21    minute            //1 byte

02   second             //1 byte

ff33>    crc          //2 byte

解决方案

NSData *initialData = [yourCharacteristic value];

A way to parse your data is to use subdataWithRange: method of NSData.

Example:

NSData *startOfFrameData = [data subdataWithRange:NSMakeRange(0, 4)];
NSLog(@"StartOfFrameData: %@", startOfFrameData);
NSData *eventIDData = [data subdataWithRange:NSMakeRange(4, 2)];
NSLog(@"eventIDData: %@", eventIDData);

etc.

Output:

>StartOfFrame: <011f6d00>
>eventIDData: <0001>

Note that I may have reversed the order of eventIDData which range could be (6,2) (instead of (4,2)), but you'll get the whole idea.

Then, you have to "understand" the meaning of the data and find the correct format, example (possible) for eventIDData:

UInt16 eventID;
[eventIDData getBytes:&eventID length:sizeof(eventID)];
NSLog(@"eventID: %d", eventID);

And so on...

If you want to "play" with it without reading again and again the characteristic value each time (which means also connect, etc.), here is a method you can use:

-(NSData *)dataWithStringHex:(NSString *)string
{
    NSString *cleanString;
    cleanString = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@">" withString:@""];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSInteger length = [cleanString length];
    uint8_t buffer[length/2];
    for (NSInteger i = 0; i < length; i+=2)
    {
        unsigned result = 0;
        NSScanner *scanner = [NSScanner scannerWithString:[cleanString substringWithRange:NSMakeRange(i, 2)]];
        [scanner scanHexInt:&result];
        buffer[i/2] = result;
    }
    return  [[NSMutableData alloc] initWithBytes:&buffer length:length/2];
}

Example use:

NSData *initialData = [self dataWithStringHex:@"<011f6d00 00011100 00000000 04050701 05000569 07df0203 020b0d21 02ff33>"]; 

That way, you can try parsing your data on an other example/project/beta test code.

这篇关于将Characterstic.Value解析为字节整数格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆