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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Android開發(fā)之ToggleButton實(shí)現(xiàn)開關(guān)效果示例
這篇文章主要介紹了Android開發(fā)之ToggleButton實(shí)現(xiàn)開關(guān)效果的方法,結(jié)合實(shí)例形式分析了ToggleButton控件實(shí)現(xiàn)開關(guān)效果的布局與功能相關(guān)操作技巧,需要的朋友可以參考下2017-07-07android實(shí)現(xiàn)音樂播放器進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)音樂播放器進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android自定義流式布局/自動(dòng)換行布局實(shí)例
這篇文章主要介紹了Android自定義流式布局/自動(dòng)換行布局實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android打開GPS導(dǎo)航并獲取位置信息返回null解決方案
最近在做一個(gè) Android 項(xiàng)目,需要用到GPS獲取位置信息,從 API 查了一下,發(fā)現(xiàn)獲取位置信息僅需極其簡(jiǎn)單的一句即可getLastKnownLocation(LocationManager.GPS_PROVIDER)郁悶的是一直為null,于是搜集整理下,曬出來與大家分享2013-01-01Android開發(fā)TextView內(nèi)的文字實(shí)現(xiàn)自動(dòng)換行
這篇文章主要為大家介紹了Android開發(fā)TextView內(nèi)的文字實(shí)現(xiàn)自動(dòng)換行,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android ProgressDialog的實(shí)例詳解
這篇文章主要介紹了Android ProgressDialog的實(shí)例詳解的相關(guān)資料,Android 開發(fā)項(xiàng)目的時(shí)候經(jīng)常會(huì)遇到耗時(shí)的操作,這里就講下Android ProgressDialog的應(yīng)用,需要的朋友可以參考下2017-07-07Android重復(fù)引用多個(gè)場(chǎng)景報(bào)錯(cuò)的問題解決
本文主要介紹了Android重復(fù)引用多個(gè)場(chǎng)景報(bào)錯(cuò)的問題解決,主要介紹了5種情況,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03