objective-cswifteddystone

iOS Eddystone beacon - Issue in finding NamespaceID


I have one old project in Objective - C. I need to convert that in latest Swift - 5. Everything is working fine except Eddystone beacons detection.

Old Objective C code,

-(NSString *) findNameSpace : (id) beaconInfo {

    ESSBeaconInfo *beacon = beaconInfo;
    ESSBeaconID *Bid = beacon.beaconID;
    NSLog(@"Beacon : %@", beacon);
    NSLog(@"Beacon ID Length : %lu",(unsigned long)Bid.beaconID.length);
    if (Bid.beaconID.length == 16) {
        NSData *d1 = [Bid.beaconID subdataWithRange:NSMakeRange(0, 10)];
        NSLog(@"Sub Data : %@", d1);
        NSString *namespace = [[NSString stringWithFormat:@"%@",d1] stringByReplacingOccurrencesOfString:@"<" withString:@""];
        namespace = [namespace stringByReplacingOccurrencesOfString:@">" withString:@""];
        namespace = [namespace stringByReplacingOccurrencesOfString:@" " withString:@""];
        NSLog(@"namespace : %@", namespace);
        return namespace;
    }

    return @"";
}

Output:-

Beacon : Eddystone, id: ESSBeaconID: beaconID= enter image description here, RSSI: -65, txPower: -31

Beacon ID Length: 16

Sub Data: enter image description here

namespace : a457ae5727fa30f48dc9

My Swift Code,

func findNameSpace(_ beaconInfo: Any) -> String? {
        let beacon: ESSBeaconInfo = beaconInfo as! ESSBeaconInfo
        let Bid: ESSBeaconID = beacon.beaconID
        print("Beacon : \(beacon)")
        print("Beacon ID Length : \(Bid.beaconID.count)")
        if Bid.beaconID.count == 16 {
            let d1 = Bid.beaconID.subdata(in: Range(NSRange(location: 0, length: 10))!)
            print("Sub Data : \(d1)")
            var namespace: String? = nil
            namespace = "\(d1)".replacingOccurrences(of: "<", with: "")
            namespace = namespace?.replacingOccurrences(of: ">", with: "")
            namespace = namespace?.replacingOccurrences(of: " ", with: "")
            print("namespace : \(namespace ?? "")")
            return namespace
        }
        return ""
    }

Output:-

Beacon : Eddystone, id: ESSBeaconID: beaconID=enter image description here, RSSI: -51, txPower: -31

Beacon ID Length: 16

Sub Data: 10 bytes

namespace : 10bytes

========================================

I am facing issue with getting namespace.. beacause you can see from above output. Data is converting in bytes in my Swift Code. I don't have any idea how to handle it. Objective C code is working perfectly fine. Any help is appreciated! Thank you.


Solution

  • This is the solution, which worked in my case.

    Posting answer here for helping those who may face this same issue. Hope it will be helpful for someone!

    func findNameSpace(_ beaconInfo: Any) -> String? {
            let beacon: ESSBeaconInfo = beaconInfo as! ESSBeaconInfo
            let Bid: ESSBeaconID = beacon.beaconID
            print("Beacon : \(beacon)")
            print("Beacon ID Length : \(Bid.beaconID.count)")
            if Bid.beaconID.count == 16 {
                let d1 = Bid.beaconID.subdata(in: Range(NSRange(location: 0, length: 10))!)
                print("Sub Data : \(d1)")
                var namespace: String? = nil
                namespace = "\(d1.hexEncodedString())".replacingOccurrences(of: "<", with: "")
                namespace = namespace?.replacingOccurrences(of: ">", with: "")
                namespace = namespace?.replacingOccurrences(of: " ", with: "")
                print("namespace : \(namespace ?? "")")
                return namespace
            }
            return ""
        }
    
    func hexEncodedString(options: HexEncodingOptions = []) -> String {
            let format = options.contains(.upperCase) ? "%02hhX" : "%02hhx"
            return map { String(format: format, $0) }.joined()
        }