How to get default gateway using Ethernet, not wifi
Asked Answered
P

4

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~~

Plunkett answered 8/6, 2012 at 2:17 Comment(0)
C
0

If you like to have a two connection at a time , then you can go to the command Prompt and check your routes that are advertised . If the Route has a single default gateway then you may connect to that particular network. If it has two default gateway then you can access both the network which is random

thanks bowmi

Core answered 8/6, 2012 at 4:41 Comment(0)
A
0

I'm assuming this is for Google-TV, it might be helpful if you added Google-TV to the title. Is there a way to statically declare the gateway on this appliance? If there is, you could try logging into your router, finding the default gateway, and then directly put the gateway into the configuration options for your device.

Abuzz answered 8/6, 2012 at 18:28 Comment(0)
J
0

Here's how you can find the IP Address on Ethernet for Google TV devices:

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();
}
Jacquetta answered 8/6, 2012 at 19:36 Comment(2)
Interesting. Out of curiosity, why the need to validate the string that's returned by getHostAddress? Shouldn't it always be valid?Cobnut
Oh. Then I suppose you could've also used instanceof Inet4AddressCobnut
R
0

This is the code we use in Able Remote. Note that we check if the interface is up (will only work with Android 2.3 and later). This is necessary since users reported devices which had multiple IP addresses but only one live at a time:

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;
    }
Reichsmark answered 20/6, 2012 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.