I am working on one BLE project I am getting following data(In Byte array while scanning BLE device) as an advertising data 0x02011A09094F6E65506C757332020AF9 you can see in the screenshot there is three packet, can anyone help me how can get all three packet by java code.
答案 0 :(得分:0)
您可以尝试以下方法:
public SparseArray<byte[]> parseAdvertisingData(byte[] rawData) {
final SparseArray<byte[]> parsedData = new SparseArray<>();
for (int index = 0; index < rawData.length; ) {
final byte dataLength = rawData[index++];
if (dataLength == 0) {
break;
}
final int dataType = rawData[index];
if (dataType == 0) {
break;
}
byte[] data = Arrays.copyOfRange(rawData, index + 1, index + dataLength);
parsedData.put(dataType, data);
index += dataLength;
}
return parsedData;
}
这是一个简单的例子:
final byte[] rawData = Utils.parseHexBinary("02011A09094F6E65506C757332020AF9");
final SparseArray<byte[]> parsed = parseAdvertisingData(rawData);
for (int i = 0; i < parsed.size(); i++) {
final int type = parsed.keyAt(i);
final byte[] data = parsed.valueAt(i);
Log.d(TAG, String.format("type: 0x%02x, value: 0x%s", type,
new BigInteger(1, data).toString(16)));
}
结果:
type: 0x01, value: 0x1a
type: 0x09, value: 0x4f6e65506c757332
type: 0x0a, value: 0xf9