package com.hjx.personalcenter.util; import android.content.Context; import android.os.Build; import android.util.Log; import com.hjx.personalcenter.model.DeviceInfo; import java.io.FileReader; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.Reader; public class GetDevicesUtil { private final static String LOG_TAG = "GetDevicesUtil"; public static DeviceInfo getDevicesInfo(Context context){ DeviceInfo mDeviceInfo = new DeviceInfo(); initDevicesInfo(mDeviceInfo,context); return mDeviceInfo; } public static void initDevicesInfo(DeviceInfo mDeviceInfo,Context context){ //if (PermissionUtil.hasReadExternalStoragePermission((Activity) context)) mDeviceInfo.setDeviceModel(android.os.Build.MODEL); Log.e(LOG_TAG,"" + mDeviceInfo.getDeviceModel()); mDeviceInfo.setMac(getMacAddress(context)); Log.e(LOG_TAG,"" + mDeviceInfo.getMac()); mDeviceInfo.setDeviceNumber(getCPUSerial(context)); Log.e(LOG_TAG,"" + mDeviceInfo.getDeviceNumber()); } private static String getCPUSerial(Context context) { //String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); String cpuAddress = Build.SERIAL; // String cpuAddress = null; // TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); // //if (PermissionUtil.hasReadExternalStoragePermission((Activity) context)) // if (mTelephony.getDeviceId() != null) { // cpuAddress = mTelephony.getDeviceId(); // } else { // //android.provider.Settings; // cpuAddress = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID); // } return cpuAddress; } private static String getMacAddress(Context mContext){ String str = ""; String macSerial = ""; try { Process pp = Runtime.getRuntime().exec( "cat /sys/class/net/wlan0/address "); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (; null != str;) { str = input.readLine(); if (str != null) { macSerial = str.trim();// 去空格 break; } } } catch (Exception ex) { ex.printStackTrace(); } if (macSerial == null || "".equals(macSerial)) { try { return loadFileAsString("/sys/class/net/eth0/address") .toUpperCase().substring(0, 17); } catch (Exception e) { e.printStackTrace(); } } return macSerial; } public static String loadFileAsString(String fileName) throws Exception { FileReader reader = new FileReader(fileName); String text = loadReaderAsString(reader); reader.close(); return text; } public static String loadReaderAsString(Reader reader) throws Exception { StringBuilder builder = new StringBuilder(); char[] buffer = new char[4096]; int readLength = reader.read(buffer); while (readLength >= 0) { builder.append(buffer, 0, readLength); readLength = reader.read(buffer); } return builder.toString(); } }