package com.doumee.keyCabinet.utils.i485; 
 | 
  
 | 
import android.content.Context; 
 | 
import android.hardware.usb.UsbConstants; 
 | 
import android.hardware.usb.UsbDevice; 
 | 
import android.hardware.usb.UsbInterface; 
 | 
import android.hardware.usb.UsbManager; 
 | 
  
 | 
import java.util.ArrayList; 
 | 
import java.util.List; 
 | 
  
 | 
public class SportUtils { 
 | 
    public static List<String> getSerialPortPaths(Context context) { 
 | 
        UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE); 
 | 
        List<UsbDevice> devices = new ArrayList<>(usbManager.getDeviceList().values()); 
 | 
        List<String> serialPaths = new ArrayList<>(); 
 | 
  
 | 
        for (UsbDevice device : devices) { 
 | 
            for (int i = 0; i < device.getInterfaceCount(); i++) { 
 | 
                UsbInterface intf = device.getInterface(i); 
 | 
                if (intf.getInterfaceClass() == UsbConstants.USB_CLASS_CDC_DATA) { 
 | 
                    serialPaths.add(device.getDeviceName()); 
 | 
                    break; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        if(serialPaths.size()==0){ 
 | 
            serialPaths.add("/dev/ttyS0"); 
 | 
            serialPaths.add("/dev/ttyS1"); 
 | 
            serialPaths.add("/dev/ttyS2"); 
 | 
            serialPaths.add("/dev/ttyS3"); 
 | 
            serialPaths.add("/dev/ttyS4"); 
 | 
            serialPaths.add("/dev/ttyS5"); 
 | 
            serialPaths.add("/dev/ttyS6"); 
 | 
            serialPaths.add("/dev/ttyS7"); 
 | 
        } 
 | 
        return serialPaths; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 字节数组转16进制 
 | 
     * @param bytes 需要转换的byte数组 
 | 
     * @return  转换后的Hex字符串 
 | 
     */ 
 | 
    public static String bytesToHexSimple(byte[] bytes) { 
 | 
        StringBuilder sb = new StringBuilder(); 
 | 
        for (byte b : bytes) { 
 | 
            sb.append(String.format("%02x", b & 0xFF)); 
 | 
        } 
 | 
        return sb.toString(); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * hex字符串转byte数组 
 | 
     * @param inHex 待转换的Hex字符串 
 | 
     * @return  转换后的byte数组结果 
 | 
     */ 
 | 
    public static byte[] hexToByteArray(String inHex){ 
 | 
        int hexlen = inHex.length(); 
 | 
        byte[] result; 
 | 
        if (hexlen % 2 == 1){ 
 | 
            //奇数 
 | 
            hexlen++; 
 | 
            result = new byte[(hexlen/2)]; 
 | 
            inHex="0"+inHex; 
 | 
        }else { 
 | 
            //偶数 
 | 
            result = new byte[(hexlen/2)]; 
 | 
        } 
 | 
        int j=0; 
 | 
        for (int i = 0; i < hexlen; i+=2){ 
 | 
            result[j]=hexToByte(inHex.substring(i,i+2)); 
 | 
            j++; 
 | 
        } 
 | 
        return result; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 校验数据是否正确 
 | 
     * @param data 
 | 
     * @return 
 | 
     */ 
 | 
    public static boolean checkData(String data){ 
 | 
        if(data.length()>2){ 
 | 
            String d = data.substring(0,data.length()-2); 
 | 
            String hz = data.substring(data.length()-2,data.length()); 
 | 
            if(hz.equals(SportUtils.getBCC(d))){ 
 | 
                return true; 
 | 
            } 
 | 
        } 
 | 
        return false; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * Hex字符串转byte 
 | 
     * @param inHex 待转换的Hex字符串 
 | 
     * @return  转换后的byte 
 | 
     */ 
 | 
    public static byte hexToByte(String inHex){ 
 | 
        return (byte)Integer.parseInt(inHex,16); 
 | 
    } 
 | 
  
 | 
    public static String getBCC(String hex) { 
 | 
        int i = 0, j = 0; 
 | 
        int len = hex.length(); 
 | 
        short inb[] = new short[len]; 
 | 
        for (i = 0; i < len; i++) { 
 | 
            inb[i] = charToHex(hex.charAt(i));   //将String里的每一个char转换为Hex 
 | 
        } 
 | 
  
 | 
        for (i = 0; i < len; i++) {    //将每两个Hex合并成一个byte 
 | 
            inb[j] = (byte) (((inb[i] << 4) & 0x00f0) | ((inb[i + 1]) & 0x000f)); 
 | 
            i++; 
 | 
            j++; 
 | 
        } 
 | 
        byte temp = 0x00; //校验值 
 | 
        for (i = 0; i < len / 2; i++) { //异或 
 | 
            temp ^= inb[i]; 
 | 
        } 
 | 
        byte[] bytes = new byte[1]; 
 | 
        bytes[0] = temp; 
 | 
        return byteToStr(bytes, bytes.length); 
 | 
    } 
 | 
    /** 
 | 
     * 接收到的字节数组转换16进制字符串 
 | 
     */ 
 | 
    public static String byteToStr(byte[] b, int size) { 
 | 
        String ret = ""; 
 | 
        for (int i = 0; i < size; i++) { 
 | 
            String hex = Integer.toHexString(b[i] & 0xFF); 
 | 
            if (hex.length() == 1) { 
 | 
                hex = '0' + hex; 
 | 
            } 
 | 
            ret += hex.toUpperCase(); 
 | 
        } 
 | 
        return ret; 
 | 
    } 
 | 
    public static short charToHex(char x) { //将单个char转换为Hex 
 | 
        short result = 0; 
 | 
        switch (x) { 
 | 
            case 'a': 
 | 
                result = 10; 
 | 
                break; 
 | 
            case 'b': 
 | 
                result = 11; 
 | 
                break; 
 | 
            case 'c': 
 | 
                result = 12; 
 | 
                break; 
 | 
            case 'd': 
 | 
                result = 13; 
 | 
                break; 
 | 
            case 'e': 
 | 
                result = 14; 
 | 
                break; 
 | 
            case 'f': 
 | 
                result = 15; 
 | 
                break; 
 | 
            case 'A': 
 | 
                result = 10; 
 | 
                break; 
 | 
            case 'B': 
 | 
                result = 11; 
 | 
                break; 
 | 
            case 'C': 
 | 
                result = 12; 
 | 
                break; 
 | 
            case 'D': 
 | 
                result = 13; 
 | 
                break; 
 | 
            case 'E': 
 | 
                result = 14; 
 | 
                break; 
 | 
            case 'F': 
 | 
                result = 15; 
 | 
                break; 
 | 
            default: 
 | 
                result = (short) Character.getNumericValue(x); 
 | 
                break; 
 | 
        } 
 | 
        return result; 
 | 
    } 
 | 
  
 | 
    public static String intToHex(int n) { 
 | 
        //StringBuffer s = new StringBuffer(); 
 | 
        StringBuilder sb = new StringBuilder(2); 
 | 
        String a; 
 | 
        char []b = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; 
 | 
        while(n != 0){ 
 | 
            sb = sb.append(b[n%16]); 
 | 
            n = n/16; 
 | 
        } 
 | 
        if(sb.length()==1){ 
 | 
            sb.append("0"); 
 | 
        } 
 | 
        a = sb.reverse().toString(); 
 | 
        return a; 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 16进制转2进制 
 | 
     * @param hexString 
 | 
     * @return 
 | 
     */ 
 | 
    public static String hexToBinary(String hexString) { 
 | 
        StringBuilder binary = new StringBuilder(); 
 | 
        for (char c : hexString.toCharArray()) { 
 | 
            int value = Character.digit(c, 16); 
 | 
            String bits = String.format("%4s", Integer.toBinaryString(value)).replace(' ', '0'); 
 | 
            binary.append(bits); 
 | 
        } 
 | 
        return binary.toString(); 
 | 
    } 
 | 
} 
 |