Get Android DhcpInfo connected via Ethernet
Asked Answered
E

1

7

How to get DhcpInfo() (gateway ip, netmask, dns, etc) of android device connected via Ethernet?

I know how to get it if device connected via Wifi and using ACCESS_WIFI_STATE permission:

 WifiManager wifi = (WifiManager) context.getSystemService(Service.WIFI_SERVICE);
 DhcpInfo dhcp = wifi.getDhcpInfo();

But I still not found the way to get it if device connected via ethernet... Thanks

Ervinervine answered 6/9, 2017 at 0:42 Comment(0)
R
8

In your AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Your code:

    ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Service.CONNECTIVITY_SERVICE);

                /* you can print your active network via using below */
                Log.i("myNetworkType: ", connectivityManager.getActiveNetworkInfo().getTypeName());
                WifiManager wifiManager= (WifiManager) getApplicationContext().getSystemService(getApplicationContext().WIFI_SERVICE);


                Log.i("routes ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getRoutes().toString());
                Log.i("domains ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDomains().toString());
                Log.i("ip address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getLinkAddresses().toString());
                Log.i("dns address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDnsServers().toString());



                if(connectivityManager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI) {
                    Log.i("myType ", "wifi");
                    DhcpInfo d =wifiManager.getDhcpInfo();
                    Log.i("info", d.toString()+"");
                }
                else if(connectivityManager.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_ETHERNET) {
/* there is no EthernetManager class, there is only WifiManager. so, I used this below trick to get my IP range, dns, gateway address etc */

                    Log.i("myType ", "Ethernet");
                    Log.i("routes ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getRoutes().toString());
                    Log.i("domains ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDomains().toString());
                    Log.i("ip address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getLinkAddresses().toString());
                    Log.i("dns address ", connectivityManager.getLinkProperties(connectivityManager.getActiveNetwork()).getDnsServers().toString());

                }
                else {

                }

Output enter image description here

You can't reach to know whether you are connected via wifi or network using WifiManager as WifiManager only deals with wifi. You have to use ConnectivityManager. I updated the code again where I merged WifiManager and ConnectivityManager to produce the result that you wanted.

Ragi answered 6/9, 2017 at 1:43 Comment(8)
but do you know how to get gateway/router ip address, netmask, dns setting when device connected via ethernet?Ervinervine
@WidhaprasaWaliprana connectivityManager.getActiveNetworkInfo().toString()Ragi
I get: [type: ETHERNET[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: (none), roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false] i don't get gateway/router ip address, netmask, dnsErvinervine
forgot to tag @uddhav-gautamErvinervine
@WidhaprasaWaliprana, if my answer solved your problem, please don't forget to accept this as a valid answer. Thanks.Ragi
think the solution is still not valid, because you dont give answer how to get it if the android device connected via ETHERNET (LAN) not WIFIErvinervine
This will only work in API 23+ as ConnectivityManager.getActiveNetwork() was added in that API.Bayer
@UddhavGautam.. In getRoute() what are the IP address are returning? Any idea?Sejant

© 2022 - 2024 — McMap. All rights reserved.