GetDevicesUtil.java
2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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();
}
}