List<int> byteArray = [];
void main() {
// 字节数组
formatByte([69, 20, 0, 14, 144, 2, 13, 224, 84, 80, 45, 76, 73, 78, 75, 95, 56, 56]);
formatByte([69, 20, 1, 14, 132, 2, 56, 56, 17, 211, 83, 105, 87, 105, 70, 105, 45, 98]);
formatByte([69, 20, 2, 14, 120, 2, 53, 55, 52, 45, 50, 46, 52, 71, 1, 208, 17, 207]);
formatByte([69, 20, 3, 14, 108, 2, 83, 105, 87, 105, 70, 105, 45, 53, 51, 48, 57, 45]);
formatByte([69, 20, 4, 14, 96, 2, 50, 46, 52, 71, 17, 207, 83, 105, 87, 105, 70, 105]);
print(byteArray);
parse();
}
void parse() {
int i = 0;
while(i < byteArray.length - 1) {
//print('i:$i');
int dataLengthIndex = i;
int rssiIndex = i+1;
int dataLen = byteArray[dataLengthIndex];
//print('dataLen:${dataLen}');
int rssi = byteArray[rssiIndex];
//print('rssi:${rssi}');
int subBegin = rssiIndex + 1;
int subEnd = rssiIndex + dataLen;
//print('subBegin:$subBegin, subEnd:$subEnd');
if (subEnd > byteArray.length - 1) {
return;
}
List<int> data = byteArray.sublist(subBegin, subEnd);
String name = String.fromCharCodes(data);
print('name:$name, data:$data');
i = subEnd;
}
}
void formatByte(List<int> bytes) {
int typeIndex = 0;
int frameCtrlIndex = 1;
int seqIndex = 2;
int dataLengthIndex = 3;
int wifiDataIndex = 6;
// 取第1位
int subtype = bytes[typeIndex] & 252;
int type = bytes[typeIndex] & 3;
//print('subtype:${subtype}, type:${type}');
bool isSlice = (bytes[frameCtrlIndex] & 16) > 0;
bool isApp = (bytes[frameCtrlIndex] & 4) > 0;
//print('isSlice:${isSlice}, isApp:${isApp}');
int seq = bytes[seqIndex];
//print('seq:${seq}');
int dataLen = bytes[dataLengthIndex];
//print('dataLen:${dataLen}');
if (dataLen != bytes.length - 4) {
return;
}
int wifiDataLen = bytes[wifiDataIndex];
//print('wifiDataLen:${wifiDataLen}');
if (isSlice) {
byteArray.addAll(bytes.sublist(6));
} else {
byteArray.addAll(bytes.sublist(4));
}
//print('byteArray:$byteArray');
}