iOS Eddystone 信标 - 查找 NamespaceID 时出现问题
iOS Eddystone beacon - Issue in finding NamespaceID
我在 Objective - C 中有一个旧项目。我需要在最新的 Swift - 5 中转换它。
除 Eddystone 信标检测外,一切正常。
旧Objective C代码,
-(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 @"";
}
输出:-
信标:Eddystone,ID:ESSBeaconID:beaconID= ,RSSI:-65,txPower:-31
信标 ID 长度:16
子数据:
命名空间:a457ae5727fa30f48dc9
我的Swift代码,
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 ""
}
输出:-
信标:Eddystone,ID:ESSBeaconID:beaconID=,RSSI:-51,txPower:-31
信标 ID 长度:16
子数据:10字节
命名空间:10 字节
========================================
我在获取命名空间时遇到问题.. 因为您可以从上面的输出中看到。在我的 Swift 代码中,数据以字节为单位进行转换。我不知道如何处理它。
Objective C 代码运行良好。
任何帮助表示赞赏!谢谢。
这是解决方案,适用于我的情况。
在这里发布答案以帮助那些可能面临同样问题的人。希望对某人有所帮助!
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, [=10=]) }.joined()
}
我在 Objective - C 中有一个旧项目。我需要在最新的 Swift - 5 中转换它。 除 Eddystone 信标检测外,一切正常。
旧Objective C代码,
-(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 @"";
}
输出:-
信标:Eddystone,ID:ESSBeaconID:beaconID=
信标 ID 长度:16
子数据:
命名空间:a457ae5727fa30f48dc9
我的Swift代码,
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 ""
}
输出:-
信标:Eddystone,ID:ESSBeaconID:beaconID=
信标 ID 长度:16
子数据:10字节
命名空间:10 字节
========================================
我在获取命名空间时遇到问题.. 因为您可以从上面的输出中看到。在我的 Swift 代码中,数据以字节为单位进行转换。我不知道如何处理它。 Objective C 代码运行良好。 任何帮助表示赞赏!谢谢。
这是解决方案,适用于我的情况。
在这里发布答案以帮助那些可能面临同样问题的人。希望对某人有所帮助!
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, [=10=]) }.joined()
}