最常用的方法,通過(guò)WiFiManager獲取:

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、雅安服務(wù)器托管、營(yíng)銷軟件、網(wǎng)站建設(shè)、張家川回族自治網(wǎng)站維護(hù)、網(wǎng)站推廣。
/**
* 通過(guò)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();
}
}這個(gè)方法Android 7.0是獲取不到的,返回的是null,其實(shí)是返回“02:00:00:00:00:00”
根據(jù)本地IP獲取:
/**
* 根據(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;
}
/**
* 獲取移動(dòng)設(shè)備本地IP
* @return
*/
private static InetAddress getLocalInetAddress() {
InetAddress ip = null;
try {
//列舉
Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
while (en_netInterface.hasMoreElements()) {//是否還有元素
NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一個(gè)元素
Enumeration<InetAddress> en_ip = ni.getInetAddresses();//得到一個(gè)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;
}這個(gè)方法Android 7.0及其以下版本都可以獲取到。
根據(jù)網(wǎng)絡(luò)接口獲取:
/**
* 通過(guò)網(wǎng)絡(luò)接口取
* @return
*/
private static String getNewMac() {
try {
List<NetworkInterface> 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地址。這個(gè)方法Android 7.0及其以下版本都可以獲取到。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
當(dāng)前名稱:Android手機(jī)獲取Mac地址的幾種方法
文章網(wǎng)址:http://www.chinadenli.net/article12/iiicdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、關(guān)鍵詞優(yōu)化、標(biāo)簽優(yōu)化、網(wǎng)站建設(shè)、App開(kāi)發(fā)、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)