2014-10-29 47 views
0

现在我正在创建一个Android应用程序,即时通讯中得到一些问题,这就是我想要的是我的设备中有 我有MAC地址ID ..在我的Android应用程序中,我想要如果该MAC地址的ID是比赛只比应用程序将运行,否则它可能不会工作。就像我们可以做或不...如何访问mac雷斯ID

我这样做是这样,但林没有得到

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

和我采取 也没有工作.. plzz帮助我先生..我想简单的代码如何访问与我的mac地址

回答

0
public static String getMACAddress(final String interfaceName) { 
    String macaddress = ""; 
    try { 
     final List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); 
     for (final NetworkInterface intf : interfaces) { 
      if (interfaceName != null) { 
       if (!intf.getName().equalsIgnoreCase(interfaceName)) continue; 
      } 
      final byte[] mac = intf.getHardwareAddress(); 
      if (mac==null) continue; 
      final StringBuilder buf = new StringBuilder(); 
      for (int idx=0; idx <= mac.length; idx++) 
       buf.append(String.format("%02X:", mac[idx])); 
      if (buf.length()>=0) buf.deleteCharAt(buf.length()-1); 
       macaddress = buf.toString(); 
      if(!macaddress.equals("")) break; 
     } 
    } catch (final Exception ex) { } 
    return macaddress ; 
}