简体   繁体   English

如何获取任何Android手机的MAC地址?

[英]How to get the MAC Address of any android phone?

I've seen this LINK on how to get the MAC Address of an android phone but in my case I need to do it in javascript or asp.net mvc3 . 我已经看到了有关如何获取Android手机的MAC地址的链接 ,但就我而言,我需要在javascriptasp.net mvc3 I Managed to get the MAC Address of the PC through MVC3 and my problem 1 is solve. 我设法通过MVC3获得了PC的MAC Address ,而我的问题1得以解决。 This is my second problem in android phones. 这是我在Android手机中的第二个问题。

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
String macAddress = wInfo.getMacAddress(); 

There is a work-around to get the Mac address in Android 6.0. 有一种变通方法来获取Android 6.0中的Mac地址。

First you need to add Internet user permission. 首先,您需要添加Internet用户权限。

Then you can find the mac over the NetworkInterfaces API. 然后,您可以通过NetworkInterfaces API找到mac。

public static String getMacAddr() {
  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 "";
          }

          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) {
  }
  return "02:00:00:00:00:00";
}

Source: http://robinhenniges.com/en/android6-get-mac-address-programmatically 来源: http//robinhenniges.com/en/android6-get-mac-address-programmatically

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM