读取Android设备的MAC地址

AndroidMACTask.java

 

package com.csdn.android.util;

import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import com.csdn.android.datamanage.module.LicenseCheck;

public class AndroidMACTask 
{
	private static boolean mInit=false;

	public static void checkDeviceMac(boolean isOk)
	{
		if(mInit)
		{
			return; 
		}
		mInit=true;
		//获取失败,则进入子线程重新获取
		if(!isOk)
		{
			//初始化Mac地址
			if(mInitMacTask==null || mInitMacTask.getStatus() == Status.RUNNING ||mInitMacTask.getStatus() == Status.FINISHED)
			{
				return;
			}
			mInitMacTask.execute();
		}
		//获取成功,并且从缓存获取
		else if(AndroidUtil.isReaderMACCache())
		{
			//读取设置Mac地址
			if(mReaderMacFromDeviceTask==null || mReaderMacFromDeviceTask.getStatus() == Status.RUNNING ||mReaderMacFromDeviceTask.getStatus() == Status.FINISHED)
			{
				return;
			}
			mReaderMacFromDeviceTask.execute();
		}
	}
	
	//读取MAC地址任务
		private static AsyncTask<Void, Void, Void> mInitMacTask = new AsyncTask<Void, Void, Void>(){

			@Override
			protected Void doInBackground(Void... params) 
			{
				boolean isOk = AndroidUtil.initMACdoInBackground();
				//假如获取MAC成功,并且设备是通过MAC授权,则重新初始化授权
				if(isOk && AndroidUtil.isReaderMac())
				{
					LicenseCheck.updateCacheState(FrameHelper.getAppContext(), false);
				}
				return null;
			}};
		
		//读取MAC地址任务
		private static AsyncTask<Void, Void, Void> mReaderMacFromDeviceTask = new AsyncTask<Void, Void, Void>(){

			@Override
			protected Void doInBackground(Void... params) 
			{
				String mac = AndroidUtil.getMacFromDeviceInBackground();
				//假如设备不是通过MAC授权的,则把物理地址放到缓存
				if(!AndroidUtil.isReaderMac())
				{
					AndroidUtil.saveMacInfo(mac);
					return null;
				}
				//MAC缓存地址同物理地址
				if(AndroidUtil.getMAC().equals(mac))
				{
					AndroidUtil.saveMacInfo(mac);
					return null;
				}
				//MAC物理地址不同与缓存地址,重新初始化
				AndroidUtil.saveMacInfo(mac);
				LicenseCheck.updateCacheState(FrameHelper.getAppContext(), false);
				return null;
			}};
			
}

AndroidUtil.java

 

 

package com.csdn.android.util;

import com.csdn.android.framework.util.StringUtil;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

public class AndroidUtil 
{
	//是否允许MAC使用MAC地址
	private static boolean ALLOW_MAC=true;
	//唯一标识的长度
	private static int IDENTIFY_LEN=14;
	//MAC地址的标识
	private static String KEY_MAC="android_mac";	
	//初始化IMEI
	private static boolean mIntiIMEI = false;
	//初始化MAC
	private static boolean mInitMAC=false;
	//IMEI缓存
	private static String mIMEI;
	//MAC地址缓存
	private static String mMAC;
	//是否读取缓存
	private static boolean mReaderCache=false;
	
	//是否读取的MAC地址
	public static boolean isReaderMac()
	{
		//没有完成
		if(!isInitCompeleted())
		{
			return false;
		}
		//不允许MAC地址
		if(!ALLOW_MAC)
		{
			return false;
		}
		//取IMEI地址
		if(!StringUtil.isNull(getIMEI()))
		{
			return false;
		}
		//取MAC地址
		return true;
	}
	
	//判断是否读取缓存
	public static boolean isReaderMACCache()
	{
		return mReaderCache;
	}
	
	//获取IMEI地址
	private static String getIMEI() {
		if (mIntiIMEI) {
			return mIMEI;
		}
		TelephonyManager telephonyManager = (TelephonyManager) FrameHelper
				.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
		try {
			mIMEI = telephonyManager.getDeviceId();
		} catch (Exception e) {
		}
		mIntiIMEI = true;
		return mIMEI;
	}
	
	//是否初始化完成
	public static boolean isInitCompeleted()
	{
		if(!StringUtil.isNull(getIMEI()) || mInitMAC)
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 获取设备唯一标识,IMEI 或者 MAC
	 * @return
	 */
	public static String getIdentifyId()
	{
		//没有完成
		if(!isInitCompeleted())
		{
			return null;
		}
		//不允许MAC地址
		if(!ALLOW_MAC)
		{
			return getIMEI();
		}
		//取IMEI地址
		if(!StringUtil.isNull(getIMEI()))
		{
			return getIMEI();
		}
		//取MAC地址
		return getMAC();
	}
	
	//获取MAC地址
	public static String getMAC() 
	{
		if (mInitMAC) 
		{
			return mMAC;
		}
		return null;
	}
	
	//获取真实MAC地址
	public static String getMACSource() 
	{
		if (mInitMAC && !StringUtil.isNull(mMAC)) 
		{
			if(mMAC.length()>12)
			{
				return mMAC.substring(0, 12);
			}
			return mMAC;
		}
		return "";
	}
	
	public static boolean initMACInMainThread()
	{
		return initMAC(1,true);
	}
	
	public static 	boolean initMACdoInBackground()
	{
		//尝试100次
		return initMAC(100,false);
	}
	
	//后台获取MAC地址
	public static String getMacFromDeviceInBackground()
	{
		return getMacFromDevice(100);
	}
	
	private static boolean initMAC(int internal,boolean readerCache)
	{
		String mac = null;
		//是否读取缓存
		if(readerCache)
		{
			//通过缓存获取
			mac = FrameHelper.readSharedData(FrameHelper.getAppContext(), KEY_MAC);
			if(!StringUtil.isNull(mac))
			{
				mInitMAC = true;
				mMAC = mac;
				mReaderCache =true;
				return true;
			}
		}
		
		mac = getMacFromDevice(internal);
		if(!StringUtil.isNull(mac))
		{
			saveMacInfo(mac);
			mReaderCache = false;
			return true;
		}
		return false;
	}
	
	//保存Mac地址
	public static void saveMacInfo(String mac)
	{
		mInitMAC = true;
		mMAC = mac;
		FrameHelper.saveSharedData(FrameHelper.getAppContext(), KEY_MAC,mMAC);
	}
	
	//尝试读取MAC地址
	private static String getMacFromDevice(int internal)
	{
		String mac=null;
		
		WifiManager wifiManager = (WifiManager) FrameHelper.getAppContext().getSystemService(Context.WIFI_SERVICE);		
		//尝试获取mac
		mac = tryGetMAC(wifiManager);
		if(!StringUtil.isNull(mac))
		{
			return mac;
		}
		
		//获取失败,尝试打开wifi获取
		boolean isOkWifi = tryOpenMAC(wifiManager);
		for(int index=0;index<internal;index++)
		{
			//如果第一次没有成功,第二次做100毫秒的延迟。
			if(index!=0)
			{
				try 
				{
					Thread.sleep(100);
				} 
				catch (InterruptedException e) 
				{
					e.printStackTrace();
				}
			}			
			mac = tryGetMAC(wifiManager);
			if(!StringUtil.isNull(mac))
			{

				break;
			}
		}
		
		//尝试关闭wifi
		if(isOkWifi)
		{
			tryCloseMAC(wifiManager);
		}	
		return mac;
	}
	
	//尝试打开MAC
	private static boolean tryOpenMAC(WifiManager manager)
	{
		boolean softOpenWifi = false;
		int state = manager.getWifiState();
		if (state != WifiManager.WIFI_STATE_ENABLED 	&& state != WifiManager.WIFI_STATE_ENABLING) 
		{
			manager.setWifiEnabled(true);
			softOpenWifi = true;
		}
		return softOpenWifi;
	}
	
	//尝试关闭MAC
	private static void tryCloseMAC(WifiManager manager)
	{
		manager.setWifiEnabled(false);
	}
	
	//尝试获取MAC地址
	private static String tryGetMAC(WifiManager manager)
	{
		WifiInfo wifiInfo = manager.getConnectionInfo();
		if (wifiInfo == null || StringUtil.isNull(wifiInfo.getMacAddress())) 
		{
			return null;
		}
		String mac = wifiInfo.getMacAddress().replaceAll(":", "").trim().toUpperCase();
		mac = formatIdentify(mac);
		return mac;
	}
	
	//格式化唯一标识
	private static String formatIdentify(String identify)
	{
		//判空
		if(StringUtil.isNull(identify))
		{
			return identify;
		}
		//去除空格
		identify = identify.trim();
		//求长度
		int len = identify.length();
		//正好
		if(len== IDENTIFY_LEN)
		{
			return identify;
		}
		//过长,截取
		if(len>IDENTIFY_LEN)
		{
			return identify.substring(0, IDENTIFY_LEN);
		}
		//过短,补0
		for(;len<IDENTIFY_LEN;len++)
		{
			identify += "0";
		}
		//大于默认
		return identify;		
	}
}



 

 

posted @ 2013-03-27 21:58  xinyuyuanm  阅读(512)  评论(0编辑  收藏  举报