I have android PC device ,handheld device which are connected through Ethernet connection. Right now i am able to get IP address and mac address of device but i also need to get the subnet mask,gateway, pri-sec DNS values. Please anyone tell me how to find these values programmatically.
The code of finding Ip Address and mac address are-
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
networkInfo = connectivityManager.getActiveNetworkInfo();
networkType = networkInfo != null ? networkInfo.getTypeName() : "";
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
ipAddress = inetAddress.getHostAddress().toString();
System.out.println("ipaddress=" + ipAddress + " formatter ip" + Formatter.formatIpAddress(inetAddress.hashCode()));
}
}
}
} catch (SocketException ex) {
}
try {
String interfaceName = "wlan0";
if (networkType.equals("ETHERNET")) {
interfaceName = "eth0";
}
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (!intf.getName().equalsIgnoreCase(interfaceName)) {
continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac == null) {
continue;
}
StringBuilder buf = new StringBuilder();
for (byte aMac : mac) {
buf.append(String.format("%02X:", aMac));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macaddress = buf.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}
Below 2 question may be feel irrelevant to you but i have them in my mind so please anyone explain them i will be thankful.
1) if i have both connection WIFI and Ethernet connection then how can i identify from which connection my application connected to server.(other then Network Type code i used)?
2) Is it possible that both WiFi and Ethernet connected in device and i can get mac address,ipaddress and all other values from both networkType?