Search code examples
network-programmingdnsdhcpgatewaywifimanager

How to collect network information programmatically


I am trying to collect network information (MAC, IP, DNS 1-2, GateWay, Subnet Mask) programmatically but I am really struggling with DNS 1-2, GateWay, and Subnet Mask. Can you give me a lead on this issue.

With wifimanager I can easly gather IP and MAC address. I can't get others.

    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    String ip = Formatter.formatIpAddress(wm.getConnectionInfo()
            .getIpAddress());
    String mac = wm.getConnectionInfo().getMacAddress();

I also can take all this information from DhcpInfo as below:

    DhcpInfo d = wm.getDhcpInfo();
    String DNS1 = d.dns1.toString();

but this returns some integer value and I can't parse it into DNS.

Is there any other way to get DNS and gateways.


Solution

  • I just editted code from selected answer in below question to get needed network information. It's not seems smart but worked for my case.

    How to configue a static IP address, netmask, gateway programmatically on Android 3.x or 4.x