2011-11-30 49 views
3

任何人都可以解釋或顯示如何獲取通過手機的便攜式WI-FI熱點連接的計算機(或其他設備)的IP地址嗎?使用手機作爲接入點的設備的IP地址

我嘗試下面的代碼從here

public String getLocalIpAddress() { 
    try { 
     for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { 
      NetworkInterface intf = en.nextElement(); 
      for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { 
       InetAddress inetAddress = enumIpAddr.nextElement(); 
       if (!inetAddress.isLoopbackAddress()) { 
        return inetAddress.getHostAddress().toString(); 
       } 
      } 
     } 
    } catch (SocketException ex) { 
     Log.e(LOG_TAG, ex.toString()); 
    } 
    return null; 
} 

但是,只返回默認網關。

我也在SO上發現another example這可能只是解決方案,但我不知道如何將它應用於我的情況。具體來說,我無法看到IP地址在哪一段代碼中。

+0

請問在這種情況下,這項工作? http://stackoverflow.com/questions/1069103/how-to-get-my-own-ip-address-in-c –

+0

不,也許我應該澄清。從我的Android應用程序中,我想檢測通過手機的便攜式WI-FI熱點連接到互聯網的設備的IP地址。您的示例顯示如何從連接的設備本身的程序中找到IP地址 – Marmoy

回答

3

檢查你的ARP表的wlan接口。 cat /proc/net/arp在命令行。

+0

這就是一個巧妙的破解 – Ronnie

5

下面的代碼會給你的IP adrress連接到Android的熱點設備

Main.java

的WiFi功能的設備&其他細節
import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
import com.whitebyte.hotspotclients.R; 
import com.whitebyte.wifihotspotutils.ClientScanResult; 
import com.whitebyte.wifihotspotutils.WifiApManager; 

public class Main extends Activity { 
     TextView textView1; 
     WifiApManager wifiApManager; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    textView1 = (TextView) findViewById(R.id.textView1); 
    wifiApManager = new WifiApManager(this); 

    scan(); 
} 

private void scan() { 
    ArrayList<ClientScanResult> clients = wifiApManager.getClientList(false); 

    textView1.append("Clients: \n"); 
    for (ClientScanResult clientScanResult : clients) { 
     textView1.append("####################\n"); 
     textView1.append("IpAddr: " + clientScanResult.getIpAddr() + "\n"); 
     textView1.append("Device: " + clientScanResult.getDevice() + "\n"); 
     textView1.append("HWAddr: " + clientScanResult.getHWAddr() + "\n"); 
     textView1.append("isReachable: " + clientScanResult.isReachable() + "\n"); 
    } 
} 

ClientScanResult.java

public class ClientScanResult { 

private String IpAddr; 

private String HWAddr; 

private String Device; 

private boolean isReachable; 

public ClientScanResult(String ipAddr, String hWAddr, String device, boolean isReachable) { 
    super(); 
    IpAddr = ipAddr; 
    HWAddr = hWAddr; 
    Device = device; 
    this.setReachable(isReachable); 
} 

public String getIpAddr() { 
    return IpAddr; 
} 

public void setIpAddr(String ipAddr) { 
    IpAddr = ipAddr; 
} 

public String getHWAddr() { 
    return HWAddr; 
} 

public void setHWAddr(String hWAddr) { 
    HWAddr = hWAddr; 
} 

public String getDevice() { 
    return Device; 
} 

public void setDevice(String device) { 
    Device = device; 
} 

public void setReachable(boolean isReachable) { 
    this.isReachable = isReachable; 
} 

public boolean isReachable() { 
    return isReachable; 
} 

}

WIFI_AP_STATE.java

 public enum WIFI_AP_STATE 
    { 
     WIFI_AP_STATE_DISABLING, WIFI_AP_STATE_DISABLED, WIFI_AP_STATE_ENABLING, WIFI_AP_STATE_ENABLED, WIFI_AP_STATE_FAILED 
    } 

WifiApManager.java

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.lang.reflect.Method; 
import java.net.InetAddress; 
import java.util.ArrayList; 
import android.content.Context; 
import android.net.wifi.WifiConfiguration; 
import android.net.wifi.WifiManager; 
import android.util.Log; 

public class WifiApManager { 
private final WifiManager mWifiManager; 

public WifiApManager(Context context) { 
    mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
} 



/** 
* Gets a list of the clients connected to the Hotspot, reachable timeout is 300 
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise 
* @return ArrayList of {@link ClientScanResult} 
*/ 
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables) { 
    return getClientList(onlyReachables, 300); 
} 

/** 
* Gets a list of the clients connected to the Hotspot 
* @param onlyReachables {@code false} if the list should contain unreachable (probably disconnected) clients, {@code true} otherwise 
* @param reachableTimeout Reachable Timout in miliseconds 
* @return ArrayList of {@link ClientScanResult} 
*/ 
public ArrayList<ClientScanResult> getClientList(boolean onlyReachables, int reachableTimeout) { 
    BufferedReader br = null; 
    ArrayList<ClientScanResult> result = null; 

    try { 
     result = new ArrayList<ClientScanResult>(); 
     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)) { 
       // Basic sanity check 
       String mac = splitted[3]; 

       if (mac.matches("..:..:..:..:..:..")) { 
        boolean isReachable = InetAddress.getByName(splitted[0]).isReachable(reachableTimeout); 

        if (!onlyReachables || isReachable) { 
         result.add(new ClientScanResult(splitted[0], splitted[3], splitted[5], isReachable)); 
        } 
       } 
      } 
     } 
    } catch (Exception e) { 
     Log.e(this.getClass().toString(), e.getMessage()); 
    } finally { 
     try { 
      br.close(); 
     } catch (IOException e) { 
      Log.e(this.getClass().toString(), e.getMessage()); 
     } 
    } 

    return result; 
} 
} 
+0

除「客戶端」以外不顯示任何內容。 – Kaushal28

0

雖然答案,如果給定的,但如果有人有興趣直接回答:

private static ArrayList<String> readArpCache() 
    { 
     ArrayList<String> ipList = new ArrayList<String>(); 
     BufferedReader br = null; 
     try { 
     br = new BufferedReader(new FileReader("/proc/net/arp"), 1024); 
     String line; 
     while ((line = br.readLine()) != null) { 
      Log.d(TAG ,line); 

      String[] tokens = line.split(" +"); 
      if (tokens != null && tokens.length >= 4) { 
       // verify format of MAC address 
       String macAddress = tokens[3]; 
       if (macAddress.matches("..:..:..:..:..:..")) { 
        //Log.i(TAG, "MAC=" + macAddress + " IP=" + tokens[0] + " HW=" + tokens[1]); 

        // Ignore the entries with MAC-address "00:00:00:00:00:00" 
        if (!macAddress.equals("00:00:00:00:00:00")) { 

        String ipAddress = tokens[0]; 
        ipList.add(ipAddress); 

        Log.i(TAG, macAddress + "; " + ipAddress); 
        } 
       } 
      } 
     } 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } finally { 
     try { 
      br.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     } 

     return ipList; 
    } 

也按照我的理解(不會自己嘗試),如果您使用aysntask並需要在工作線程上執行(不會在主線程上工作),那麼也可以使用此過程。無論是使用方法找到hotspotinterface或直接使用_hotspotInterface爲「爲wlan0」

try { 
     InetAddress[] arr = InetAddress.getAllByName(_hotspotInterface); 

     } catch (UnknownHostException e) { 
     e.printStackTrace(); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 

要找到熱點我用這個方法(雖然不是很好的例子)

String _hotspotInterface = ""; 
    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) 
      { 
       NetworkInterface anInterface = en.nextElement(); 

       //look for 192.168.xx.xx first 
       for (Enumeration<InetAddress> enumIpAddr = anInterface.getInetAddresses(); enumIpAddr.hasMoreElements();) 
       { 
        InetAddress inetAddress = enumIpAddr.nextElement(); 
        String  hostAddress = inetAddress.getHostAddress(); 

        if (hostAddress.startsWith("192.168.")) 
        { 
         _hotspotInterface = anInterface.getName(); 
        } 
       } 
} 
相關問題