GetDevicesUtil.java 1.76 KB
package com.hjx.personalcenter.util;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.hjx.personalcenter.model.DeviceInfo;


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){
		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 cpuAddress = null;
		TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
		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  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;		
	}
}