tags:

views:

52

answers:

1

Hi,

I´m trying to get hold of the addresses to the currently used DNS-servers in my application, either I´m connected thru Wifi or mobile. The DhcpInfo object should provide this but how can I get hold of a valid DhcpInfo object?

Best regards John

+3  A: 

android.net.ConnectivityManager will deliver you an array of NetworkInfo's using getAllNetworkInfo(). Then use android.net.NetworkUtils.runDhcp() to get a DhcpInfo for any given network interface - the DhcpInfo structure has the IP address for dns1 and dns2 for that interface (which are integer values representing the IP address).

In case you are wondering how the hell you are going to transform the integer into an IP address, you can do this:

/**
* Convert int IP adress to String 
* cf. http://teneo.wordpress.com/2008/12/23/java-ip-address-to-integer-and-back/
*/
private String intToIp(int i) {
    return ( i & 0xFF) + "." +
        (( i >> 8 ) & 0xFF) + "." +
        (( i >> 16 ) & 0xFF) + "." +
        (( i >> 24 ) & 0xFF);
}

Edit

You can also get a DchcpInfo object by doing something like this:

WiFiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE); 
DhcpInfo info = wifi.getDhcpInfo();
Cristian
But isn´t android.net.NetworkUtils.runDhcp() a native function? How do I access it?
John
Ok, getting the DhcpInfo from Wifi is straight forward but how do you do it for mobile? (UMTS etc.) Do I have to load some native library to get access to android.net.NetworkUtils.runDhcp() ?
John