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
版權宣告:本文為博主原創文章,轉載請附上博文連結!