androidandroid-wifiandroid-internetandroid-wireless

Android - How to format Internet properties from Dhcp?


I am new in android and I am creating an application in which I am showing LAN IP Address, Subnet Mask, Default Gateway and other information I got IP Address using the following code

try{
    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
    TextView ipAddressText = (TextView)findViewById(R.id.productInfo_lanIpAddress);
    ipAddressText.setText(ip);
    DhcpInfo dInfo = wm.getDhcpInfo();

    ip = String.valueOf(dInfo.gateway);
    TextView defaultGateway = (TextView)findViewById(R.id.productInfo_defaultGateway);
        defaultGateway.setText(ip);
    }//*/
    catch (Exception ex) {
        TextView ipAddressText = (TextView)findViewById(R.id.productInfo_lanIpAddress);
        ipAddressText.setText(ex.toString());

    }
    /*

Now I believe I am getting the Default gateway but it is formatted in numbers so it is not showing properly (I am getting 16885352) is there a way like Formatter which we used to Format IP Address? I'd also like to get a link or guide on how to achieve the same effect on other Internet information. Thanks!


Solution

  • Ok So I found a way to achieve that it is done similarly to how we get the IP Address. i.e, use Formatter.formatIpAddress() to get the proper Subnet Mask, Default Gateway, Primary DNS and Secondary DNS. Figured Since they were in same format. Cheers!