gpt4 book ai didi

ios - 在 Swift 上从 kCBAdvDataManufacturerData 中提取数据

转载 作者:可可西里 更新时间:2023-11-01 17:03:58 45 4
gpt4 key购买 nike

我有一个 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> 并在屏幕上显示每个字段。具体来说,这就是我的案例中的数字:

  1. 0d00 - TI 制造商 ID
  2. fe - 我给的节点ID
  3. 05 - 节点状态(保持不变
  4. c6f - 传感器标签电池电压
  5. 32-是BLE数据包计数器。

在 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/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com