Android手機(jī)獲取Mac地址的幾種方法
最常用的方法,通過WiFiManager獲?。?/p>
/**
* 通過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獲?。?/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;
}
/**
* 獲取移動(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ò)接口獲?。?/p>
/**
* 通過網(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é)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Android實(shí)現(xiàn)微信側(cè)滑刪除當(dāng)前頁面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信側(cè)滑刪除當(dāng)前頁面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android?studio實(shí)現(xiàn)app登錄界面
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)app登錄界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android studio button 按鈕 四種綁定事件的方法【實(shí)例代碼】
這篇文章主要介紹了Android studio button 按鈕 四種綁定事件的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
微信小程序電商常用倒計(jì)時(shí)實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了微信小程序電商常用倒計(jì)時(shí)實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)帶有進(jìn)度條的按鈕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05
Android ViewPager動(dòng)態(tài)加載問題
這篇文章主要介紹了Android ViewPager動(dòng)態(tài)加載問題,需要的朋友可以參考下2017-03-03
Android App中ListView仿QQ實(shí)現(xiàn)滑動(dòng)刪除效果的要點(diǎn)解析
這篇文章主要介紹了Android App中ListView仿QQ實(shí)現(xiàn)滑動(dòng)刪除效果的要點(diǎn)解析,重點(diǎn)是要判斷手勢(shì)按下的位置坐標(biāo),需要的朋友可以參考下2016-04-04
android實(shí)現(xiàn)點(diǎn)擊按鈕切換不同的fragment布局
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)點(diǎn)擊按鈕切換不同的fragment布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

