GetDevicesUtil.java
1.9 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
package com.hjx.personalcenter.util;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import com.hjx.personalcenter.model.DeviceInfo;
public class GetDevicesUtil {
	private final static String LOG_TAG = "GetDevicesUtil";
	public static String getDevicesJson(Context context){
		DeviceInfo mDeviceInfo = new DeviceInfo();
		initDevicesInfo(mDeviceInfo,context);
		StringBuilder sb = new StringBuilder();		 
		sb.append("\"devices\":[{");		
		sb.append("\"deviceNumber\":").append("\"").append( mDeviceInfo.getDeviceNumber()).append("\",");
		sb.append("\"deviceModel\":").append("\"").append(mDeviceInfo.getDeviceModel()).append("\",");		
		sb.append("\"mac\":").append("\"").append(mDeviceInfo.getMac()).append("\",");
		sb.append("}]");
		Log.i(LOG_TAG, sb.toString());
		return sb.toString();
	}
	public static DeviceInfo getDevicesInfo(Context context){
		DeviceInfo mDeviceInfo = new DeviceInfo();
		initDevicesInfo(mDeviceInfo,context);
		return mDeviceInfo;
	}
	public static void initDevicesInfo(DeviceInfo mDeviceInfo,Context 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.getDeviceModel());
	}	 
	private static String getCPUSerial(Context context) {
		return MachineUtil.getMachineCode(context);
	}
	private static String getMacAddress(Context mContext){
		String  ret = null;
		try {
			WifiManager manager = (WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = manager.getConnectionInfo();
			ret = info.getMacAddress();
		} catch (Exception e) {
			Log.e(LOG_TAG, "get wifi address wrong", e);
		}
		Log.i(LOG_TAG, "wifi address is" + ret);
		return ret;		
	}
}