2013-07-02 59 views

回答

2

您可以在接入點算連接的設備,它的下面鏈接得到的android硬件MAC地址: http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/

從上面的鏈接代碼:

/** 
* Try to extract a hardware MAC address from a given IP address using the 
* ARP cache (/proc/net/arp).<br> 
* <br> 
* We assume that the file has this structure:<br> 
* <br> 
* IP address  HW type  Flags  HW address   Mask  Device 
* 192.168.18.11 0x1   0x2   00:04:20:06:55:1a  *  eth0 
* 192.168.18.36 0x1   0x2   00:22:43:ab:2a:5b  *  eth0 
* 
* @param ip 
* @return the MAC from the ARP cache 
*/ 
public static String getMacFromArpCache(String ip) { 
    if (ip == null) 
     return null; 
    BufferedReader br = null; 
    try { 
     br = new BufferedReader(new FileReader("/proc/net/arp")); 
     String line; 
     while ((line = br.readLine()) != null) { 
      String[] splitted = line.split(" +"); 
      if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) { 
       // Basic sanity check 
       String mac = splitted[3]; 
       if (mac.matches("..:..:..:..:..:..")) { 
        return mac; 
       } else { 
        return null; 
       } 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      br.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return null; 
} 

或者如果u有代碼試試問題以下代碼:

public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) { 
     ArrayList<InetAddress> ret = new ArrayList<InetAddress>(); 

     LoopCurrentIP = 0; 

     String IPAddress = ""; 
     String[] myIPArray = YourPhoneIPAddress.split("\\."); 
     InetAddress currentPingAddr; 


     for (int i = 0; i <= 255; i++) { 
      try { 

       // build the next IP address 
       currentPingAddr = InetAddress.getByName(myIPArray[0] + "." + 
         myIPArray[1] + "." + 
         myIPArray[2] + "." + 
         Integer.toString(LoopCurrentIP)); 
       ad = currentPingAddr.toString(); ///////////////// 
       Log.d("MyApp",ad);     ////////////// 

       // 50ms Timeout for the "ping" 
       if (currentPingAddr.isReachable(50)) { 

        ret.add(currentPingAddr); 
        ad = currentPingAddr.toString();  ///////////////// 
        Log.d("MyApp",ad);      ////////////// 
       } 
      } catch (UnknownHostException ex) { 
      } catch (IOException ex) { 
      } 

      LoopCurrentIP++; 
     } 
     return ret; 
    } 
+0

這不起作用。我設置了手機的AP並將筆記本電腦連接到AP。當我執行'cat/proc/net/arp'時,我看到一個記錄包含我的筆記本電腦的MAC和IP,這是正確的。問題是,當我將筆記本電腦與AP斷開連接時,ARP緩存不會刷新,並且執行上一個命令仍然會記錄一條記錄。 –

+0

@MridangAgarwalla我更新了我的回答試試這個! –

+0

您是否嘗試過我的解決方案? –

相關問題