问题描述
如何获取通过以太网连接的android设备的DhcpInfo()(网关ip,netmask,dns等)?
How to get DhcpInfo() (gateway ip, netmask, dns, etc) of android device connected via Ethernet?
如果设备通过Wifi并使用ACCESS_WIFI_STATE权限进行连接,我知道如何获取它:
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
推荐答案
在您的 AndroidManifest.xml 文件中
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
您的代码:
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 {
}
输出
您无法得知您是通过WifiManager
通过wifi还是通过网络连接的,因为WifiManager
仅适用于wifi.您必须使用ConnectivityManager
.我在合并WifiManager
和ConnectivityManager
的地方再次更新了代码,以产生所需的结果.
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.
这篇关于通过以太网获取连接的Android DhcpInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!