Dhugalmac
Dhugalmac

Reputation: 574

iOS Swift Value from [String : AnyObject]

I am trying to get the value out of an [String : AnyObject] and have not yet found the answer on a web-reference

One of the parameters in a method call is:

advertisementData: [String : AnyObject]

And, when I run the code, a debug line print("\(advertisementData)") shows one of its values as:

["kCBAdvDataIsConnectable": 1, "kCBAdvDataServiceUUIDs": <__NSArrayM 0x13cd812e0>( C9CAB9B8-3ABF-4043-A5AF-9AD00C6074D5]

On each pass, I am trying to identify the value of the key:
kCBAdvDataServiceUUIDs -- it often changes on each pass through the code

I am looking to see if the value contains: C9CAB9B8-3ABF-4043-A5AF-9AD00C6074D5 (as this example does) - many times it does not.
If so I will do something. If not do nothing.

I have tried:

let value = advertisementData["kCBAdvDataServiceUUIDs"]  

but it did not work and the error message was not clear (at least for this newbie it wasn't clear)

How can I get this value into a variable so that I can compare it to another String value?

Upvotes: 0

Views: 1405

Answers (4)

Kiran Jadhav
Kiran Jadhav

Reputation: 3327

Updated for swift :

use below simple code;

    let yourServiceUUIDString = "FFF0"
    if let
        mAdvData = advertisementData["kCBAdvDataServiceUUIDs"] as? [AnyObject], (mAdvData.contains { ($0 as? CBUUID)?.uuidString == yourServiceUUIDString}) {
        print("BLE device found..!")
    }

Upvotes: 1

Lepidopteron
Lepidopteron

Reputation: 6175

At first, you could just limit your scanning results to the specific Service UUID. Might ensure in very crowded bluetooth areas that you will not be spammed with all the others in your CBCentralManager delegate.

let serviceUUID = CBUUID(string: "C9CAB9B8-3ABF-4043-A5AF-9AD00C6074D5")
self.centralManager?.scanForPeripherals(withServices: [serviceUUID], options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])

Or in your CBPeripheralDelegate:

func centralManager(_ central: CBCentralManager,
                        didDiscover peripheral: CBPeripheral,
                        advertisementData: [String : Any],
                        rssi RSSI: NSNumber)
    {
        print("peripheral: \(peripheral)")
        print("     \(advertisementData)")
        print (RSSI)

        if let uuids = advertisementData[CBAdvertisementDataServiceUUIDsKey] as? [AnyObject],
            uuids.contains(where:{ ($0 as? CBUUID)?.uuidString == "C9CAB9B8-3ABF-4043-A5AF-9AD00C6074D5" }) {
            central.connect(peripheral, options: nil)
        }
    }

Upvotes: 0

Luca Angeletti
Luca Angeletti

Reputation: 59536

This code should work

import CoreBluetooth

func foo(advertisementData: [String : AnyObject]) {

    if let
        list = advertisementData["kCBAdvDataServiceUUIDs"] as? [AnyObject]
        where (list.contains { ($0 as? CBUUID)?.UUIDString == "C9CAB9B8-3ABF-4043-A5AF-9AD00C6074D5" }) {
        print("Found")
    }
}

Upvotes: 4

chengsam
chengsam

Reputation: 7405

let value = advertisementData["kCBAdvDataServiceUUIDs"]  as! [String]
let result = value.contains("C9CAB9B8-3ABF-4043-A5AF-9AD00C6074D5")
print(result ? "Found" : "Not Found")

Upvotes: 0

Related Questions