0

我正在将广告数据从嵌入式设备发送到 iPad。此数据标记为“服务数据”-AD 类型 (0x16)。当我与代理 didDiscoverPeripheral一起阅读广告数据时,我得到以下信息:

 Adv.-Data: {
kCBAdvDataServiceData =     {
    "Unknown (<fdf0>)" = <01020305>;
};
kCBAdvDataServiceUUIDs =     (
    "Unknown (<fdf0>)"
);

服务 0xFDF0 由我选择。现在我需要获取字节0x010x020x030x05,它们是服务 0xFDF0 的数据,键为 kCBAdvDataServiceData

如果我之后有一个包含这 4 个字节的数组,那将是完美的。我尝试了很长时间,但我得到的最接近的是字符串"Unknown (< fdf0>)" = <01020305>

4

2 回答 2

0

假设您的 Adv 数据字典称为 current_adv_dic:

NSArray *aa4 = [current_adv_dic valueForKey:@"kCBAdvDataServiceData"];
NSString *ss2 = [NSString stringWithFormat:@"%@",aa4];
Byte AdvDataArray[ss2.length];
NSLog(@"AdvDataArray: ");
for(int i=0; i<ss2.length; i++){
    AdvDataArray[i]=[ss2 characterAtIndex:i];
    printf("%x,",AdvDataArray[i]);
}
printf("\r\n");

请注意,第一个字节是 '{' 字符,然后是 0x0a 用于换行,然后是空格等。示例:7b,a,20,20,20,20,22,55,6e,6b,6e,6f ,77,6e,20,28,3c,然后您会找到您的服务数据..然后您可以通过传统方法将 UniChars 转换为字节/十六进制值。

于 2013-11-08T17:49:33.530 回答
0
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSString *serviceData = [advertisementData valueForKey:@"kCBAdvDataServiceData"];
    NSString *devInfo = [self serviceDataValue:@"Device Information" withData:serviceData];

    devInfo = [[devInfo stringByReplacingOccurrencesOfString:@" " withString:@""] uppercaseString];

    NSLog(@"ServiceData: %@", serviceData);
    NSLog(@"Data: %@", devInfo);
}

- (NSString *)serviceDataValue:(NSString *)key withData:(NSString *)data
{
    if (!data) return NULL;

    data = [NSString stringWithFormat:@"%@", data];

    NSError *error = NULL;
    NSString *pattern = [NSString stringWithFormat:@"\"%@\" = <(.*)>", key];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    if (!error)
    {
        NSTextCheckingResult *match =
        [regex firstMatchInString:data options:0 range:NSMakeRange(0, data.length)];

        if (match.numberOfRanges > 1)
        {
            return [data substringWithRange:[match rangeAtIndex:1]];
        }

    }

    return NULL;
}
于 2015-05-21T03:39:25.837 回答