/*
class PacketHandler {
private static final byte PACKET_START = 0x79; // 假设包的开始标记为0x79
private static final byte PACKET_END = 0x0D; // 假设包的结束标记为0x0D
private static ByteArrayOutputStream buffer = new ByteArrayOutputStream();public static void onDataReceived(byte[] data) {buffer.write(data, 0, data.length);byte[] completePacket = null;while ((completePacket = findNextPacket(buffer.toByteArray())) != null) {// 处理完整的包processPacket(completePacket);// 从缓冲区中移除已处理的包removeProcessedPacket(buffer, completePacket);}
}private static byte[] findNextPacket(byte[] data) {int startIndex = -1;for (int i = 0; i < data.length; i++) {if (data[i] == PACKET_START) {startIndex = i;break;}}if (startIndex >= 0) {int endIndex = -1;for (int i = startIndex; i < data.length; i++) {if (data[i] == PACKET_END) {endIndex = i;break;}}if (endIndex > startIndex) {return Arrays.copyOfRange(data, startIndex, endIndex + 1);}}return null;
}static StringBuffer stringBuffer = new StringBuffer();
static boolean isComnplete;private static void processPacket(byte[] packet) {// 处理包的逻辑String s = MyFunc.ByteArrToNoSpaceHex(packet);stringBuffer.append(s);try {
// Log.i(“byteDataResult”, stringBuffer.toString() + “=111=” + fromHexString(stringBuffer.toString()));
} catch (Exception e) {
e.printStackTrace();
}
if (stringBuffer != null && stringBuffer.toString().endsWith("23796F6E6B65720D")) {String dateTemp = stringBuffer.toString();int startPosition = dateTemp.lastIndexOf("796F6E6B657223");String date = dateTemp.substring(startPosition);stringBuffer = new StringBuffer();isComnplete = true;if (isComnplete) {isComnplete = false;Log.i("byteDataResult", "===" + date);}}
}public static String fromHexString(String hexString) throws Exception {String result = "";hexString = hexString.toUpperCase();String hexDigital = "0123456789ABCDEF";char[] hexs = hexString.toCharArray();byte[] bytes = new byte[hexString.length() / 2];int n;for (int i = 0; i < bytes.length; i++) {n = hexDigital.indexOf(hexs[2 * i]) * 16 + hexDigital.indexOf(hexs[2 * i + 1]);bytes[i] = (byte) (n & 0xff);}result = new String(bytes, "UTF-8");return result;
}private static void removeProcessedPacket(ByteArrayOutputStream buffer, byte[] packet) {byte[] newData = new byte[buffer.size() - packet.length];int packetStart = 0;for (int i = 0; i < buffer.size(); i++) {if (i < packet.length) {packetStart = i;} else {newData[i - packet.length] = buffer.toByteArray()[i];}}buffer.reset();buffer.write(newData, 0, newData.length);
}
}*/