5

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?

shyam002
  • 237
  • 1
  • 5
  • 19

1 Answers1

-3
private static String intToIP(int ipAddress) {
String ret = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
  (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
  (ipAddress >> 24 & 0xff));
   return ret;
}

public  String GetSubnetMask_WIFI() {
   //  wifii= (WifiManager) 
  getCurrentActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  WifiInfo wifiInfo = wifiManager.getConnectionInfo();

  DhcpInfo dhcp = wifiManager.getDhcpInfo();
  String mask = intToIP(dhcp.netmask);

  return mask;
}
HamidReza
  • 1
  • 3
  • 2
    i am already getting subnet and gateway from WIFI network and in my question i asked from ethernet. – shyam002 Sep 10 '18 at 07:11