- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 TI 传感器标签作为外围设备,它以 kCBAdvDataManufacturerData 的形式广播 BLE 数据。我想在 iOS 中从这些数据中提取不同的值。
我正在 Swift 中执行以下操作:
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){
for (index, foundPeripheral) in peripherals.enumerated(){
if foundPeripheral.peripheral?.identifier == peripheral.identifier{
peripherals[index].lastRSSI = RSSI
print("AdvertisementData:\(advertisementData)")
return
}
}
let isConnectable = advertisementData["kCBAdvDataIsConnectable"] as! Bool
let displayPeripheral = DisplayPeripheral(peripheral: peripheral, lastRSSI: RSSI, isConnectable: isConnectable)
peripherals.append(displayPeripheral)
tableView.reloadData()
}
}
这是我在控制台中看到的:
广告数据:[“kCBAdvDataIsConnectable”:0,“kCBAdvDataManufacturerData”:<0d00fe05 0c6f32>,“kCBAdvDataLocalName”:CLIMBC]
我对解码感兴趣的数据是 kCBAdvDataManufacturerData : <0d00fe05 0c6f32> 并在屏幕上显示每个字段。具体来说,这就是我的案例中的数字:
在 Android 中我可以解码如下:
private static String getNodeIdFromRawPacket(byte[] manufSpecField) {
if(manufSpecField != null && manufSpecField.length > 1) {
return String.format("%02X", manufSpecField[0]);
}else{
return null;
}
}
private static int getNodeBatteryVoltageFromRawPacket(byte[] manufSpecField){
if(manufSpecField != null && manufSpecField.length > 4) {
return (((((int) manufSpecField[manufSpecField.length - 3]) << 24) >>> 24) << 8) + ((((int) manufSpecField[manufSpecField.length - 2]) << 24) >>> 24);
}else{
return 0;
}
}
private byte[] extractManufacturerSpecificData(byte[] scanRecord, int manufacturer_id){
if(scanRecord != null) {
int ptr = 0;
while (ptr < scanRecord.length && scanRecord[ptr] != 0) {
int field_length = scanRecord[ptr];
if (scanRecord[ptr + 1] == (byte) (0xFF)) { //this is true when the manufacturer specific data field has been found
if (((scanRecord[ptr + 3] << 8) + scanRecord[ptr + 2]) == manufacturer_id) {
byte[] manufacturerSpecificData = new byte[field_length - 3];
System.arraycopy(scanRecord, ptr + 4, manufacturerSpecificData, 0, field_length - 3);
return manufacturerSpecificData;
}
}
ptr += (field_length + 1);
}
return null;
}else{
return null;
}
}
};
我究竟怎样才能做到这一点?我是 Swift 的新手,这就是我遇到一些困难的原因。任何代码片段都将受到欢迎。
最佳答案
查看控制台的输出,advertisementData["kCBAdvDataManufacturerData"]
似乎是一个包含 7 个字节的 NSData
。您可以轻松地将其作为 Swift Data
访问,并且 Data
中的每个字节都可以使用下标访问:
if let manufacturerData = advertisementData["kCBAdvDataManufacturerData"] as? Data {
assert(manufacturerData.count >= 7)
//0d00 - TI manufacturer ID
//Constructing 2-byte data as little endian (as TI's manufacturer ID is 000D)
let manufactureID = UInt16(manufacturerData[0]) + UInt16(manufacturerData[1]) << 8
print(String(format: "%04X", manufactureID)) //->000D
//fe - the node ID that I have given
let nodeID = manufacturerData[2]
print(String(format: "%02X", nodeID)) //->FE
//05 - state of the node (something that remains constant
let state = manufacturerData[3]
print(String(format: "%02X", state)) //->05
//c6f - is the sensor tag battery voltage
//Constructing 2-byte data as big endian (as shown in the Java code)
let batteryVoltage = UInt16(manufacturerData[4]) << 8 + UInt16(manufacturerData[5])
print(String(format: "%04X", batteryVoltage)) //->0C6F
//32- is the BLE packet counter.
let packetCounter = manufacturerData[6]
print(String(format: "%02X", packetCounter)) //->32
}
关于ios - 在 Swift 上从 kCBAdvDataManufacturerData 中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140636/
我们通过使用 centralManager:didDiscoverPeripheral:advertisementData:RSSI 获取正在广播的 BLE 制造商数据方法。 显然,到达kCBAdvD
我有一个 Broadcom BLE 外设广播数据,其中包括以制造商数据形式发布的序列号。当我尝试在 iOS (Swift) 中获取它时,我很难弄清楚如何将它变成变量或常量。 我能够执行: printl
我有一个 TI 传感器标签作为外围设备,它以 kCBAdvDataManufacturerData 的形式广播 BLE 数据。我想在 iOS 中从这些数据中提取不同的值。 我正在 Swift 中执行以
我尝试了很多东西,但这似乎是我能得到的最接近 NSDictionary advertisementData = e.AdvertisementData; NSString ns_p_mdata = n
我是一名优秀的程序员,十分优秀!