Android 物联网一般数据解析

跟硬件那边传输数据,一般都是十六进制传输的数据,所以一般先解析出来十六进制的字符串后者十六进制的字符数组

收到原始数据:

byte[] buffer = new byte[] {104, 56, 56, 104, 0, 114, 120, 85, 52, 18, 67, 35, 1, 7, 0, 0, 0, 0, 12, 19, 120, 86, 52, 18, 12,

59, 120, 52, 18, 12, 38, 120, 86, 52, 18, 11, 89, 69, 35, 0, 2, -3, 23, 0, 0, 22};


    /**
     * byte数组转换成16进制字符数组
     * 
     * @param src
     * @return
     */
    public static String[] bytesToHexStrings(byte[] src) {
        if (src == null || src.length <= 0) {
            return null;
        }
 
        String[] str = new String[src.length];
 
        for (int i = 0; i < src.length; i++) {
            str[i] = String.format("%02X", src[i]);
        }
        
        return str;
    }

然后就是根据不同位数提取有用的数据


            //总流量
            if (strResult.length != 0) {
                if (strResult[19].equalsIgnoreCase("0C") && strResult[20].equalsIgnoreCase("13")) {
                    String strWater = strResult[24] + strResult[23] + strResult[22] + strResult[21];
                    double dWater = Double.valueOf(strWater);
                    mtextViewShow1.setText(String.valueOf(dWater/1000) + "m³");
                }
 
                //流速
                if (strResult[25].equalsIgnoreCase("0C") && strResult[26].equalsIgnoreCase("3B")) {
                    String strFlow = strResult[30]+strResult[29]+strResult[28]+strResult[27];
                    double dFlow = Double.valueOf(strFlow);
                    
                    mtextViewShow2.setText(String.valueOf(dFlow/1000) + "m³/h");                    
                }
 
                //温度
                if (strResult[37].equalsIgnoreCase("0B") && strResult[38].equalsIgnoreCase("59")) {
                    String strTemp = strResult[41]+strResult[40]+strResult[39];
                    double dTemp = Double.valueOf(strTemp);
                    
                    mtextViewShow5.setText(String.format("%.2f", dTemp/100) + "℃");
                }
 
                if (strResult[42].equalsIgnoreCase("02")
                        && strResult[43].equalsIgnoreCase("FD") && strResult[44].equalsIgnoreCase("17")) {
                    String hexResult = StringUtil.reverseString(StringUtil.hexStringToBinary(strResult[45]));
                    
                    if(hexResult == null){
                        return;
                    }
                    
                    String str = hexResult.substring(5, 6);
                    
                    //0表示管道正常           倒数第三位
                    if (str.equalsIgnoreCase("0")) {
                        mtextViewShow3.setText("Normal");
                    }
                    //1表示空管;
                    else if (str.equalsIgnoreCase("1")) {
                        mtextViewShow3.setText("Empty");
                    }
                    
                    // 最后两位
                    String str1 = hexResult.substring(6, 8);
                    //01 表示静止
                    if (str1.equalsIgnoreCase("01") || str1.equalsIgnoreCase("11")) {
                        mtextViewShow4.setText("Flowing");
                    }
                    //00 表示启动
                    if (str1.equalsIgnoreCase("00")) {
                        mtextViewShow4.setText("Start");
                    }
                    //10,11表示流动
                    if (str1.equalsIgnoreCase("10")) {
                        mtextViewShow4.setText("Stagnant");
                    }
                }
            }

都是根据约定提取数据啦

最后附上一个常用进制转换工具类


package com.aufw.util;
 
import java.io.ByteArrayOutputStream;
 
public class StringUtil {
    //  十六进制的字符串转换成byte数组    
    public static byte[] HexCommandtoByte(byte[] data) {
        if (data == null) {
            return null;
        }
        int nLength = data.length; 
        
        String strTemString = new String(data, 0, nLength);
        String[] strings = strTemString.split(" ");
        nLength = strings.length;
        data = new byte[nLength];            
        for (int i = 0; i < nLength; i++) {
            if (strings[i].length() != 2) {
                data[i] = 00;
                continue;
            }
            try {
                data[i] = (byte)Integer.parseInt(strings[i], 16);
            } catch (Exception e) {
                data[i] = 00;
                continue;
            }
        }
    
        return data;
    }
    
    /**
     * byte数组转换成16进制字符数组
     * 
     * @param src
     * @return
     */
    public static String[] bytesToHexStrings(byte[] src) {
        if (src == null || src.length <= 0) {
            return null;
        }
 
        String[] str = new String[src.length];
 
        for (int i = 0; i < src.length; i++) {
            str[i] = String.format("%02X", src[i]);
        }
        
        return str;
    }
    
    /**
     * 十六进制字符串装十进制
     * 
     * @param hex
     *            十六进制字符串
     * @return 十进制数值
     */
    public static int hexStringToAlgorism(String hex) {
        if (hex == null) {
            return 0;
        }
        hex = hex.toUpperCase();
        int max = hex.length();
        int result = 0;
        for (int i = max; i > 0; i--) {
            char c = hex.charAt(i - 1);
            int algorism = 0;
            if (c >= '0' && c <= '9') {
                algorism = c - '0';
            } else {
                algorism = c - 55;
            }
            result += Math.pow(16, max - i) * algorism;
        }
        return result;
    }
    
    // 十六进制的字符串转化为String
    private static String hexString = "0123456789ABCDEF";
    public static String decode(String bytes) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(
                bytes.length() / 2);
        // 将每2位16进制整数组装成一个字节
        for (int i = 0; i < bytes.length(); i += 2)
            baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
                    .indexOf(bytes.charAt(i + 1))));
        return new String(baos.toByteArray());
    }
    
      /**
     * 十六转二进制
     * 
     * @param hex
     *            十六进制字符串
     * @return 二进制字符串
     */
    public static String hexStringToBinary(String hex) {
           if (hex == null) {
            return null;
        }
        hex = hex.toUpperCase();
        String result = "";
        int max = hex.length();
        for (int i = 0; i < max; i++) {
            char c = hex.charAt(i);
            switch (c) {
            case '0':
                result += "0000";
                break;
            case '1':
                result += "0001";
                break;
            case '2':
                result += "0010";
                break;
            case '3':
                result += "0011";
                break;
            case '4':
                result += "0100";
                break;
            case '5':
                result += "0101";
                break;
            case '6':
                result += "0110";
                break;
            case '7':
                result += "0111";
                break;
            case '8':
                result += "1000";
                break;
            case '9':
                result += "1001";
                break;
            case 'A':
                result += "1010";
                break;
            case 'B':
                result += "1011";
                break;
            case 'C':
                result += "1100";
                break;
            case 'D':
                result += "1101";
                break;
            case 'E':
                result += "1110";
                break;
            case 'F':
                result += "1111";
                break;
            }
        }
        return result;
    }
    
    /** 
     * 倒置字符串 
     *  
     * @param str 
     * @return 
     */  
    public static String reverseString(String str)  
    {  
        char[] arr=str.toCharArray();  
        int middle = arr.length>>1;//EQ length/2  
        int limit = arr.length-1;  
        for (int i = 0; i < middle; i++) {  
            char tmp = arr[i];  
            arr[i]=arr[limit-i];  
            arr[limit-i]=tmp;  
        }  
        return new String(arr);  
    } 
}

关于物联网的解析数据可查看这个
android byte字节数组转换十六进制字符串(物联网开发总结)
--------------------- 
作者:mmsx 
来源:CSDN 
原文:https://blog.csdn.net/qq_16064871/article/details/53428031 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/leansmall/article/details/83962208