- 易迪拓培训,专注于微波、射频、天线设计工程师的培养
IOS如何获取广播数据?
IOS 怎么获取外设的广播数据AdvData
(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ ……}
这个方法中的advertisementData只能抓取到下面的键值
{
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = SimpleBLEPeripheral;
kCBAdvDataServiceUUIDs = ( FFF0 );
kCBAdvDataTxPowerLevel = 0;
}
simpleBLEPeripheral.c的 avertData数组的内容,也就是下图报文中的AdvData
NSArray *keys = [advertisementData allKeys];
NSData *dataAmb, *dataObj;
for (int i = 0; i < [keys count]; ++i) {
id key = [keys objectAtIndex: i];
NSString *keyName = (NSString *) key;
NSObject *value = [advertisementData objectForKey: key];
if ([value isKindOfClass: [NSArray class]]) {
printf(" key: %s\n", [keyName cStringUsingEncoding: NSUTF8StringEncoding]);
NSArray *values = (NSArray *) value;
for (int j = 0; j < [values count]; ++j) {
if ([[values objectAtIndex: j] isKindOfClass: [CBUUID class]]) {
CBUUID *uuid = [values objectAtIndex: j];
NSData *data = uuid.data;
if (j == 0) {
dataObj = uuid.data;
} else {
dataAmb = uuid.data;
}
printf(" uuid(%d):", j);
for (int j = 0; j < data.length; ++j)
printf(" %02X", ((UInt8 *) data.bytes)[j]);
printf("\n");
} else {
const char *valueString = [[value description] cStringUsingEncoding: NSUTF8StringEncoding];
printf(" value(%d): %s\n", j, valueString);
}
}
} else {
const char *valueString = [[value description] cStringUsingEncoding: NSUTF8StringEncoding];
printf(" key: %s, value: %s\n", [keyName cStringUsingEncoding: NSUTF8StringEncoding], valueString);
}
}