0

I am working on android system application. I would like to know if IP address is DHCP or STATIC.

Do we have any android java class for this? Or is there any way to get it from sysfs like /sysfs/class/net/eth0?

jww
  • 97,681
  • 90
  • 411
  • 885
Ranjan Kumar
  • 1,164
  • 7
  • 12
  • Since you are entertaining Java answers... also see [How to know ip address of the router from code in android?](https://stackoverflow.com/q/9035784/608639), [Get my Wifi ip address Android](https://stackoverflow.com/q/16730711/608639) and [Programmatically determine if ip address is assigned via dhcp or manually set in Java](https://stackoverflow.com/q/12541186/608639) – jww Aug 23 '19 at 11:57
  • Thanks jww. It will work for WiFi. In my use case i have Ethernet interface also and i wanted to get the addressingType based on current active network. Android has made the 'Etherenet Service'(android.net.EthernetManager) as hidden class, so i am not able to move further. – Ranjan Kumar Aug 26 '19 at 09:37

2 Answers2

1

I used this to check wether is DHCP or STATIC years ago, perhaps you can try it out, if it does not work I'll remove the answer.

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration wificonf : configuredNetworks){
    if (wificonf.networkId == connectionInfo.getNetworkId()){
         if (wificonf.toString().toLowerCase().indexOf("DHCP".toLowerCase())>-1){
            //DHCP
        }else if(wificonf.toString().toLowerCase().indexOf("STATIC".toLowerCase())>-1){
            //STATIC
        }
            break;
    }
}
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • Thanks Skizo. It will work for WiFi. In my use case i have Ethernet interface also and i wanted to get the addressingType based on current active network. Android has made the 'Etherenet Service'(android.net.EthernetManager) as hidden class, so i am not able to move further. – Ranjan Kumar Aug 26 '19 at 09:36
0

If I go into Settings on my Android Samsung phone (as an example) and click on Connections, it will bring up the WiFi that it is using. Then click on the Settings (on my phone it is a Settings icon next to the WiFi name) and it will bring up more information about your connection. There you need to select "View More" and then you will see if it is set to DHCP or Static.