[英]Convert CBUUID to NSNumber in swift
How Can I convert a CBUUID to UINT16?如何将 CBUUID 转换为 UINT16?
I want to procure the device information in the advertisement data as UINT16.我想获取广告数据中的设备信息作为 UINT16。
I am getting the error我收到错误
Could not cast value of type 'CBUUID' (0x1d2715e68) to 'NSNumber' (0x1d25c9db0).无法将“CBUUID”(0x1d2715e68)类型的值转换为“NSNumber”(0x1d25c9db0)。 2019-12-04 14:17:18.731697-0500 ProjectName[717:125723] Could not cast value of type 'CBUUID' (0x1d2715e68) to 'NSNumber' (0x1d25c9db0). 2019-12-04 14:17:18.731697-0500 ProjectName[717:125723] 无法将“CBUUID”(0x1d2715e68)类型的值转换为“NSNumber”(0x1d25c9db0)。
My Advertisement Data in terminal我在终端中的广告数据
[ [
"kCBAdvDataTxPowerLevel": 0, "kCBAdvDataTxPowerLevel": 0,
"kCBAdvDataIsConnectable": 1, "kCBAdvDataIsConnectable": 1,
"kCBAdvDataTimestamp": 597179838.727399, “kCBAdvDataTimestamp”:597179838.727399,
"kCBAdvDataServiceUUIDs": <__NSArrayM 0x281252e20>( 22043200-9530-4EA8-9D21-04146852E51F ) , “kCBAdvDataServiceUUIDs”:<__NSArrayM 0x281252e20>(22043200-9530-4EA8-9D21-04146852E51F),
"kCBAdvDataServiceData": { "Device Information" = {length = 6, bytes = 0x010000020004 }; “kCBAdvDataServiceData”:{“设备信息”={长度=6,字节=0x010000020004 }; }, },
"kCBAdvDataLocalName": base0 “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!!!谢谢你!!!
As per [the documentation[( https://developer.apple.com/documentation/corebluetooth/cbadvertisementdataservicedatakey ), the dictionary associated with CBAdvertisementDataServiceDataKey
is of type <CBUUID,Data>
.根据 [文档[( https://developer.apple.com/documentation/corebluetooth/cbadvertisementdataservicedatakey ),与CBAdvertisementDataServiceDataKey
关联的字典的类型为<CBUUID,Data>
。 Having retrieved the dictionary you can then subscript it using the appropriate CBUUID
instance (0x180A is the Device Information service).检索字典后,您可以使用适当的CBUUID
实例(0x180A 是设备信息服务) CBUUID
下标。
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.