I use the connection manager class and get the network info, I just get if network type is Wifi or ethernet. But When my network type is Wifi, I would like to know if its static or dynamic.
Can someone help me out with this
I use the connection manager class and get the network info, I just get if network type is Wifi or ethernet. But When my network type is Wifi, I would like to know if its static or dynamic.
Can someone help me out with this
below You can find simple (not optimised) code adapted from this thread. Code is written for Android 4.0.
Remember to add additional user permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Method:
public String getIpAssignment(){
String myenumvalue="";
WifiConfiguration wifiConf = null;
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks){
if (conf.networkId == connectionInfo.getNetworkId()){
wifiConf = conf;
break;
}
}
if(wifiConf != null){
try {
Object Enumer = getDeclaredField(wifiConf, "ipAssignment");
myenumvalue = Enumer.toString();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return myenumvalue;
}
It returns one of enum fields from ipAssignment: UNASSIGNED | STATIC | DHCP
my android test device android version 5.1.1(embedded board) I used the code above, but it fell into the error ("catch NoSuchFieldException")
i use this code to find static or dhcp connection:
public void getWifiDhcpStatic(){
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
for (WifiConfiguration conf : configuredNetworks){
if (conf.networkId == connectionInfo.getNetworkId()){
if (conf.toString().toLowerCase().indexOf("DHCP".toLowerCase())>-1){
Log.i("myLogDHCPorSTATIC","DHCP ");
}else if(conf.toString().toLowerCase().indexOf("STATIC".toLowerCase())>-1){
Log.i("myLogDHCPorSTATIC","STATIC ");
}
break;
}
}
}
Do not forget to add in manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />