亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android手機(jī)獲取Mac地址的幾種方法

 更新時(shí)間:2019年02月21日 15:41:07   作者:十一月Siy  
今天小編就為大家分享一篇關(guān)于Android手機(jī)獲取Mac地址的幾種方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

最常用的方法,通過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)文章

最新評(píng)論