Java中Byte接收数据缓存解析方法

package cn.yq.aio.ui.main.xuehongdanbai;

import com.clj.fastble.utils.BinaryUtil;
import com.clj.fastble.utils.HexUtil;
import com.creative.FingerOximeter.IFingerOximeterCallBack;
import com.creative.FingerOximeter.Verifier;
import com.creative.base.BaseDate;
import com.creative.base.BaseProtocol;
import com.creative.base.BaseThread;
import com.creative.base.Ianalyse;
import com.creative.base.Receive;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import cn.yq.aio.ui.main.xueyang.sdk.StatusMsg;

public class XueHongSuAnalyseData extends BaseThread implements Ianalyse {
    private static final String TAG = "XueHongSuAnalyseData";
    public static Object lock = new Object();
    public static Vector originalData = new Vector();
    private IFingerOximeterCallBack callBack;

    public XueHongSuAnalyseData() {
//        this.callBack = callBack;
    }

    public void addData(byte[] data){
        synchronized (lock) {
            for(int i = 0; i < data.length; ++i) {
                originalData.add(data[i]);
            }
        }
        System.out.println("XueHongSuAnalyseData addData");
    }

    public void run() {
        super.run();
        try {
            while (!this.stop) {
//                synchronized (this) {
                    System.out.println("analyse");
//                    if (this.pause) {
//                        try {
//                            wait();
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                    }
                    if (lock != null) {
                        synchronized (lock) {
                            if (originalData.size() > 0) {
                                System.out.println("333333333");
                                analyse();
                                System.out.println("44444444");
                            }
                        }
                        Thread.sleep(100L);
                    }
//                }
                System.out.println("synchronized exit");
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }
    private boolean checkIntactCnt(Vector originalData){
        int dataSize = originalData.size();
        Object [] tt =originalData.toArray();
        ByteBuffer buffer=ByteBuffer.allocate(128);
        for (Object o:tt){
            buffer.put((byte)o);
        }
        System.out.println("Receive.originalData cnt,data:"+ HexUtil.formatHexString(buffer.array()));
        buffer.clear();
        while (dataSize>4){
            if (originalData.get(0)==(byte)0x24 &&
                    originalData.get(1)==(byte)0x50 &&
                    originalData.get(2)==(byte)0x43 &&
                    originalData.get(3)==(byte)0x4C){
                if (dataSize>6){
                    if (originalData.get(4)==(byte)0xE1 &&
                            originalData.get(5)==(byte)0x00){
                        //血红蛋白
                        if (dataSize>10){
                            int dataLength = originalData.get(8).byteValue()&0xff+(originalData.get(9).byteValue()&0xff)*256;
                            if (dataSize>=10+dataLength+1){
                                //一包完整的数据
                                return true;
                            }else {
                                return false;
                            }
                        }else {
                            return false;
                        }
                    }else {
                        originalData.remove(0);
                    }
                }else {
                    return false;
                }
            }else {
                originalData.remove(0);
            }
            dataSize = originalData.size();
        }
        return false;
    }
    public void analyse(){
        boolean exit = checkIntactCnt(originalData);
        try {
            if (exit){
    //            2450434CE10000000900000015011D13118C00AC
                for (int i = 0; i < 8; i++) {
                    originalData.remove(0);
                }
                int dataLength = originalData.remove(0).byteValue()&0xff+(originalData.remove(0).byteValue()&0xff)*256;
                originalData.remove(0);
                originalData.remove(0);
                int year=originalData.remove(0).byteValue();
                int month=originalData.remove(0).byteValue();
                int dayOfMonth=originalData.remove(0).byteValue();
                int hour=originalData.remove(0).byteValue();
                int minute=originalData.remove(0).byteValue();
                int value=originalData.remove(0).byteValue()&0xff+(originalData.remove(0).byteValue()&0xff)*256;
                originalData.remove(0);
                System.out.println("year:"+year+",month:"+month+",dayOfMonth:"+dayOfMonth+",hour:"+hour+",minute:"+minute+",value:"+value);
            }else {
                System.out.println("不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(算法,java,缓存,开发语言)