1

i'm looking forward to know how to programmatically find the default gateway Address. i've already found default gateway Address for Wifi-Manager(getDhcpInfo()), but i don't find it in Ethernet mode. please. Help me~~

4

4 回答 4

0

我假设这是针对 Google-TV 的,如果您将 Google-TV 添加到标题中可能会有所帮助。有没有办法在这个设备上静态声明网关?如果有,您可以尝试登录路由器,找到默认网关,然后直接将网关放入设备的配置选项中。

于 2012-06-08T18:28:07.110 回答
0

以下是在以太网上查找 Google TV 设备 IP 地址的方法:

private static final String IPADDRESS_PATTERN =
        "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";

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();
                String ipAddress = inetAddress.getHostAddress().toString();
                if (!inetAddress.isLoopbackAddress()
                        && validate(ipAddress)) {

                    return ipAddress;
                }
            }
        }
    } catch (SocketException e) {
        // TODO(mjoshi): Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}


/**
 * Validate ip address with regular expression
 * 
 * @param ip ip address for validation
 * @return true valid ip address, false invalid ip address
 */
public boolean validate(final String ip) {
    Pattern pattern = Pattern.compile(IPADDRESS_PATTERN);
    Matcher  matcher = pattern.matcher(ip);
    return matcher.matches();
}
于 2012-06-08T19:36:04.990 回答
0

这是我们在 Able Remote 中使用的代码。请注意,我们检查接口是否已启动(仅适用于 Android 2.3 及更高版本)。这是必要的,因为用户报告了具有多个 IP 地址但一次只有一个存在的设备:

public static String getLocalIpAddress() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                if (intf.isUp()) {
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        if (!inetAddress.isLoopbackAddress()) {
                            if (inetAddress instanceof Inet4Address) { // only want ipv4 address
                                return inetAddress.getHostAddress().toString();
                            }
                        }
                    }
                }
            }
        } catch (Throwable e) {
            Log.e(LOG_TAG, "Failed to get the IP address", e);
        }

        return null;
    }
于 2012-06-20T16:55:21.297 回答
0

如果您希望一次有两个连接,那么您可以转到命令提示符并检查您公布的路由。如果路由有一个默认网关,那么您可以连接到该特定网络。如果它有两个默认网关,那么您可以访问两个随机的网络

谢谢鲍米

于 2012-06-08T04:41:49.690 回答