GetDevicesUtil.java 1.9 KB
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;		
	}
}