新聞中心
最常用的方法,通過WiFiManager獲?。?/p>

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請域名、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、張家川回族自治網(wǎng)站維護(hù)、網(wǎng)站推廣。
/**
* 通過WiFiManager獲取mac地址
* @param context
* @return
*/
private static String tryGetWifiMac(Context context) {
WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wi = wm.getConnectionInfo();
if (wi == null || wi.getMacAddress() == null) {
return null;
}
if ("02:00:00:00:00:00".equals(wi.getMacAddress().trim())) {
return null;
} else {
return wi.getMacAddress().trim();
}
}這個方法Android 7.0是獲取不到的,返回的是null,其實(shí)是返回“02:00:00:00:00:00”
根據(jù)本地IP獲?。?/p>
/**
* 根據(jù)IP地址獲取MAC地址
* @return
*/
private static String getLocalMacAddressFromIp() {
String strMacAddr = null;
try {
//獲得IpD地址
InetAddress ip = getLocalInetAddress();
byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < b.length; i++) {
if (i != 0) {
buffer.append(':');
}
String str = Integer.toHexString(b[i] & 0xFF);
buffer.append(str.length() == 1 ? 0 + str : str);
}
strMacAddr = buffer.toString().toUpperCase();
} catch (Exception e) {
}
return strMacAddr;
}
/**
* 獲取移動設(shè)備本地IP
* @return
*/
private static InetAddress getLocalInetAddress() {
InetAddress ip = null;
try {
//列舉
Enumeration en_netInterface = NetworkInterface.getNetworkInterfaces();
while (en_netInterface.hasMoreElements()) {//是否還有元素
NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一個元素
Enumeration en_ip = ni.getInetAddresses();//得到一個ip地址的列舉
while (en_ip.hasMoreElements()) {
ip = en_ip.nextElement();
if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
break;
else
ip = null;
}
if (ip != null) {
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ip;
} 這個方法Android 7.0及其以下版本都可以獲取到。
根據(jù)網(wǎng)絡(luò)接口獲?。?/p>
/**
* 通過網(wǎng)絡(luò)接口取
* @return
*/
private static String getNewMac() {
try {
List all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return null;
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:", b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
} 注意網(wǎng)絡(luò)接口的Name有跟多:dummy0、p2p0、wlan0....其中wlan0就是我們需要WiFi mac地址。這個方法Android 7.0及其以下版本都可以獲取到。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
新聞名稱:Android手機(jī)獲取Mac地址的幾種方法
文章鏈接:http://www.dlmjj.cn/article/iiicdc.html


咨詢
建站咨詢
