2013-07-02 2 views

답변

2

액세스 포인트에 연결된 장치를 셀 수 그것은 안드로이드에 링크 아래에 하드웨어 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

내 솔루션을 사용해 보셨습니까? –

관련 문제