3

在 centralManager:didDiscoverPeripheral:advertisementData:RSSI(下面的完整代码)中,我在 NSDictionary 中找到了一个名为 kCBAdvDataServiceUUIDs 的键。我正在尝试读取这些数据以确定设备上可用的服务。这个数据是什么格式的?类描述很简单

Unknown (<fff0>)

这是来源:

- (void) centralManager: (CBCentralManager *) central
  didDiscoverPeripheral: (CBPeripheral *) aPeripheral
      advertisementData: (NSDictionary *) advertisementData
                   RSSI: (NSNumber *) RSSI
{
printf("Discovered %s\n", [[aPeripheral name] cStringUsingEncoding: NSUTF8StringEncoding]); // TODO: Remove printfs
    printf("  RSSI: %s\n", [[RSSI stringValue] cStringUsingEncoding: NSUTF8StringEncoding]);
    NSArray *keys = [advertisementData allKeys];
    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) {
                NSObject *aValue = [values objectAtIndex: j];
                printf("       %s\n", [[aValue description] cStringUsingEncoding: NSUTF8StringEncoding]);
                printf("       is NSData: %d\n", [aValue isKindOfClass: [NSData class]]);
            }
        } else {
            const char *valueString = [[value description] cStringUsingEncoding: NSUTF8StringEncoding];
            printf("   key: %s, value: %s\n", [keyName cStringUsingEncoding: NSUTF8StringEncoding], valueString);
        }
    }
}

以下是 TI CC2250 迷你开发套件中密钥卡的输出:

Discovered (null)
  RSSI: -36
   key: kCBAdvDataServiceUUIDs
       Unknown (<fff0>)
       is NSData: 0
Discovered SimpleBLEPeripheral
  RSSI: -37
   key: kCBAdvDataServiceUUIDs
       Unknown (<fff0>)
       is NSData: 0
   key: kCBAdvDataLocalName, value: SimpleBLEPeripheral
   key: kCBAdvDataTxPowerLevel, value: 0
4

1 回答 1

14

经过一番挖掘,这就是我发现的:

作为参数传递给 centralManager:didDiscoverPeripheral:advertisementData:RSSI 的广告数据是一个 NSDictionary,它似乎总是包含至少一个名为 kCBAdvDataServiceUUIDs 的键。与此键关联的值是 CBUUID 类型的对象的 NSArray。

CBUUID is not documented in the iOS 5 documentation, despite the fact that it is used in many places, including every Apple sample for Bluetooth LE I've seen. Among it's methods is one called data that returns an NSData object. This NSData object has the UUID encoded as a series of bytes.

So, to get and, in this case, view the bytes in the UUIDs of a BLE advertisement, you can use code such as the following:

- (void) centralManager: (CBCentralManager *) central
  didDiscoverPeripheral: (CBPeripheral *) aPeripheral
      advertisementData: (NSDictionary *) advertisementData
                   RSSI: (NSNumber *) RSSI
{
    printf("Discovered %s\n", [[aPeripheral name] cStringUsingEncoding: NSUTF8StringEncoding]); // TODO: Remove printfs
    printf("  RSSI: %s\n", [[RSSI stringValue] cStringUsingEncoding: NSUTF8StringEncoding]);
    NSArray *keys = [advertisementData allKeys];
    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;
                    printf("      uuid(%d):", j);
                    for (int j = 0; j < data.length; ++j)
                        printf(" %2X", ((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);
        }
    }
}
于 2012-07-23T17:53:06.120 回答