2

My peripheral advertises one serviceUUID as part of the advertisementData. I want the advertised serviceUUID without connecting. Here is my code:

func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {

        peripherals.append(peripheral)

        let serviceUUID = [advertisementData: CBAdvertisementDataServiceUUIDsKey]

        print(serviceUUID)
    }

When my code runs I see this in the console:

    {
        {
        kCBAdvDataIsConnectable = 1;
        kCBAdvDataLocalName = "sensor ID not set up yet";
        kCBAdvDataServiceUUIDs =         (
            "926D74A0-D820-1000-8000-00805F9B34FB"
        );
    } = kCBAdvDataServiceUUIDs;
}

I am after the "926D74A0-D820-1000-8000-00805F9B34FB". Any ideas how to get it? Thanks.

Pat Dolan
  • 21
  • 3

2 Answers2

0

Well, there is small bug in your code .

let serviceUUID = [advertisementData: CBAdvertisementDataServiceUUIDsKey] basically creates a new dictionary with a single key-value pair. The key is the original advertisementData dictionary and the corresponding value is CBAdvertisementDataServiceUUIDsKey - which is what you see in your log.

What you want to do is:

let serviceUUID = advertisementData[CBAdvertisementDataServiceUUIDsKey]

Hope that helps!

Jens Meder
  • 4,237
  • 1
  • 25
  • 25
0

You're asking a good question, because this is a real pain point: cryptic output in the debugger at the Swift/Obj-C intersection.

The actual issue is that the value returned by advertisementData[CBAdvertisementDataServiceUUIDsKey] is an NSArray. How do I know?

(lldb) po advertisementData
▿ 2 elements
  ▿ 0 : 2 elements
    - key : "kCBAdvDataIsConnectable"
    - value : 1
  ▿ 1 : 2 elements
    - key : "kCBAdvDataServiceUUIDs"
    ▿ value : 1 element
      - 0 : 926D74A0-D820-1000-8000-00805F9B34FB

(lldb) po advertisementData[CBAdvertisementDataServiceUUIDsKey]
▿ Optional<Any>
  ▿ some : 1 element
    - 0 : 926D74A0-D820-1000-8000-00805F9B34FB

OK so we see it's an optional. Add !:

(lldb) po advertisementData[CBAdvertisementDataServiceUUIDsKey]!
▿ 1 element
  - 0 : 926D74A0-D820-1000-8000-00805F9B34FB

And we're back where we started. What the heck is this? Use type(of:):

(lldb) po type(of:advertisementData[CBAdvertisementDataServiceUUIDsKey]!)
__NSArrayM

That cryptic type means NSMutableArray. So we need to cast the output to NSArray and index the array:

(lldb) po advertisementData[CBAdvertisementDataServiceUUIDsKey]! as! NSArray
▿ 1 element
  - 0 : 926D74A0-D820-1000-8000-00805F9B34FB

(lldb) po (advertisementData[CBAdvertisementDataServiceUUIDsKey]! as! NSArray)[0]
926D74A0-D820-1000-8000-00805F9B34FB

What's the type?

(lldb) po type(of:(advertisementData[CBAdvertisementDataServiceUUIDsKey]! as! NSArray)[0])
CBUUID

OK so we've got a CBUUID. We're done:

(lldb) po ((advertisementData[CBAdvertisementDataServiceUUIDsKey]! as! NSArray)[0]) as! CBUUID
926D74A0-D820-1000-8000-00805F9B34FB

Ugh.

bcattle
  • 12,115
  • 6
  • 62
  • 82