使用电话作为接入点的设备的 IP 地址 [英] IP address of device using phone as access point

查看:25
本文介绍了使用电话作为接入点的设备的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释或展示如何获取通过手机的便携式 WI-FI 热点连接的计算机(或其他设备)的 IP 地址?

Can anyone explain or show how to get the IP address of a computer (or other device) that's connected through the phone's portable WI-FI hotspot?

我从 这里尝试了以下代码

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;
}

但这只会返回默认网关.

But that only returns the default gateway.

我还发现 另一个例子在这里,它可能只是解决方案,但我不知道如何将它应用于我的情况.具体来说,我看不到那段代码中 IP 地址的位置.

I also found another example here on SO, and it might just be the solution, but I don't know how to apply it to my situation. Specifically I cannot see where the IP address is in that piece of code.

推荐答案

下面的代码会给你ip地址&连接到 android 热点设备的 wifi 启用设备的其他详细信息

The following code will give you the ip adrress & other details of the wifi enabled devices connected to the the android hotspot device

Main.java

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: 
");
    for (ClientScanResult clientScanResult : clients) {
        textView1.append("####################
");
        textView1.append("IpAddr: " + clientScanResult.getIpAddr() + "
");
        textView1.append("Device: " + clientScanResult.getDevice() + "
");
        textView1.append("HWAddr: " + clientScanResult.getHWAddr() + "
");
        textView1.append("isReachable: " + clientScanResult.isReachable() + "
");
    }
}

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;
}
}

这篇关于使用电话作为接入点的设备的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆