0

How Can I convert a CBUUID to UINT16?

I want to procure the device information in the advertisement data as UINT16.

I am getting the error

Could not cast value of type 'CBUUID' (0x1d2715e68) to 'NSNumber' (0x1d25c9db0). 2019-12-04 14:17:18.731697-0500 ProjectName[717:125723] Could not cast value of type 'CBUUID' (0x1d2715e68) to 'NSNumber' (0x1d25c9db0).

My Advertisement Data in terminal

[
"kCBAdvDataTxPowerLevel": 0,
"kCBAdvDataIsConnectable": 1,
"kCBAdvDataTimestamp": 597179838.727399,
"kCBAdvDataServiceUUIDs": <__NSArrayM 0x281252e20>( 22043200-9530-4EA8-9D21-04146852E51F ) ,
"kCBAdvDataServiceData": { "Device Information" = {length = 6, bytes = 0x010000020004}; },
"kCBAdvDataLocalName": base0
]

I have highlighted the information which I need in Bold.

I have the following code below.

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

        guard peripheral.state != .connected else {
            return
        }


        let advertisementDataService = advertisementData[CBAdvertisementDataServiceDataKey]
        let deviceInformation = advertisementDataService as! Dictionary<UInt16,AnyObject>
}

Thank You!!!

Denzil
  • 415
  • 5
  • 13
  • 1
    You can't. A CBUUID is a 128 bit identifier. A UInt16 is a 16 bit number. What are you trying to do? – Paulw11 Dec 04 '19 at 20:19
  • I want to access the byte information from "kCBAdvDataServiceData": { "Device Information" = {length = 6, bytes = 0x010000020004}; }, and assign this to a variable and publish it to my project because this is a package and then store it in Database. So whenever I scan any peripheral the peripheral should match my the value from database and then connect after scanning.This is part of my requirement. If you have a better solution to achieve this please let me know. – Denzil Dec 04 '19 at 20:27
  • 1
    You need to specify your dictionary as `` as per the documentation https://developer.apple.com/documentation/corebluetooth/cbadvertisementdataservicedatakey – Paulw11 Dec 04 '19 at 20:42
  • Thanks @Paulw11 but I have an error.... Cannot subscript a value of type '[String : Any]' with an argument of type 'CBUUID'... I cannot subscript the information. – Denzil Dec 05 '19 at 14:15
  • Sorry, there was a typo. I updated the answer – Paulw11 Dec 05 '19 at 19:33

1 Answers1

0

As per [the documentation[(https://developer.apple.com/documentation/corebluetooth/cbadvertisementdataservicedatakey), the dictionary associated with CBAdvertisementDataServiceDataKey is of type <CBUUID,Data>. Having retrieved the dictionary you can then subscript it using the appropriate CBUUID instance (0x180A is the Device Information service).

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

    guard peripheral.state != .connected else {
        return
    }


    if let deviceInformation = advertisementData[CBAdvertisementDataServiceDataKey] as? Dictionary<CBUUID,Data>, 
       let data = deviceInformation[CBUUID(string:"0x180A")] {
        // Do something with the data
    }
}
Paulw11
  • 108,386
  • 14
  • 159
  • 186