读取Android设备的MAC地址
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;
}
}