2014-10-29 18 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 ; 
}