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

Android手機獲取Mac地址的方法

 更新時間:2017年05月22日 17:15:42   作者:十一月Siy  
這篇文章主要為大家詳細介紹了Android手機獲取Mac地址的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最常用的方法,通過WiFiManager獲取:

/** 
   * 通過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,其實是返回“02:00:00:00:00:00”

根據(jù)本地IP獲?。?br />

/** 
   * 根據(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<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces(); 
      while (en_netInterface.hasMoreElements()) {//是否還有元素 
        NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一個元素 
        Enumeration<InetAddress> 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ò)接口獲?。?br />

/** 
 * 通過網(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地址。這個方法Android 7.0及其以下版本都可以獲取到。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論