2

I want to create a "simple" app that will find all IP addresses in a given network..

For example...

I have a button that when clicked, find all IP addresses and display them in a ListView..

Does anyone have an example or functional source code that I can explain how to do ?

Sorry for my english =)

Anfuca
  • 1,329
  • 1
  • 14
  • 27
Edoardo Goffredo
  • 303
  • 2
  • 5
  • 20
  • following url may be help you http://stackoverflow.com/questions/3345857/how-to-get-a-list-of-ip-connected-in-same-network-subnet-using-java – Dhaval Solanki Mar 29 '16 at 07:33

2 Answers2

6

This is my code for finding IP addresses in local network. First you have to find your device IP Address then ping each ip addresses using subnetof that ip address. Suppose device ip address is 192.168.0.100 then local subnet is 192.168.0.. and then ping each IP addresses from 192.168.0.1 to 192.168.0.255 for finding ip address of device you can use two method: 1 Method:

    String ip = "";
      Enumeration<NetworkInterface> enumNetworkInterfaces =  NetworkInterface.getNetworkInterfaces();

           while(enumNetworkInterfaces.hasMoreElements())
           {
            NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
     Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();

      while(enumInetAddress.hasMoreElements())
            {
             InetAddress inetAddress = enumInetAddress.nextElement();
    String ipAddress = "";
    if(inetAddress.isSiteLocalAddress())
             {
              ipAddress = "SiteLocalAddress: ";
    }
     ip += ipAddress + inetAddress.getHostAddress() + "\n";
String subnet = getSubnetAddress(ip);

private String getSubnetAddress(int address) 
     {
         String ipString = String.format(
                   "%d.%d.%d",
                   (address & 0xff),
                   (address >> 8 & 0xff),
                   (address >> 16 & 0xff));

                   return ipString;
     }

2 Method:

 WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo  mWifiInfo = mWifiManager.getConnectionInfo();
String subnet = getSubnetAddress(mWifiManager.getDhcpInfo().gateway);

After finding subnet address, ping each IP addresses in network:

 private void checkHosts(String subnet)
     {
         try 
         {
             int timeout=5;
             for (int i=1;i<255;i++)
             {
                 String host=subnet + "." + i;
                 if (InetAddress.getByName(host).isReachable(timeout))
                 {
                     Log.d(TAG, "checkHosts() :: "+host + " is reachable");
                 }
             }
         }
         catch (UnknownHostException e) 
         {
             Log.d(TAG, "checkHosts() :: UnknownHostException e : "+e);
             e.printStackTrace();
         } 
         catch (IOException e) 
         {
             Log.d(TAG, "checkHosts() :: IOException e : "+e);
             e.printStackTrace();
         }
    }

After pinging each IP Addresses then android kernel file stored those IP address who are in networks. You can get this list by calling AndroidOS file. This file is arp which is stored in /proc/net/ in android. You can get in through the code. Just execute particular command programatically and stored in your modelData then notify your listView Through Adapter:

ArrayList<IpAddress> mIpAddressesList;
    private boolean getIpFromArpCache() 
         {
             BufferedReader br = null;
             char buffer[] = new char[65000];
             String currentLine;
             try 
             {
                 br = new BufferedReader(new FileReader("/proc/net/arp"));

                 while ((currentLine = br.readLine()) !=  null) 
                 {
                     Log.d(TAG, "getIpFromArpCache() :: "+ currentLine);

                     String[] splitted = currentLine.split(" +");
                     if (splitted != null && splitted.length >= 4) 
                     {
                        String ip = splitted[0];
                        String mac = splitted[3];
                        if (!splitted[3].equals(emptyMac)) 
                        {
                            if (!splitted[0].equals(IP)) 
                            {
    //                          int remove = mac.lastIndexOf(':');
    //                          mac = mac.substring(0,remove) + mac.substring(remove+1);
                                mac = mac.replace(":", "");
                                Log.i(TAG, "getIpFromArpCache() :: ip : "+ip+" mac : "+mac);
                                mIpAddressesList.add(new IpAddress(ip, mac));
                            }
                        }
                     }

                 }

                 return true;

             } 
             catch (Exception e) 
             {
                 e.printStackTrace();
             } 
             finally 
             {
                 try 
                 {
                     br.close();
                 } 
                 catch (IOException e) 
                 {
                     e.printStackTrace();
                 }
             }
             return false;
         }



public class IpAddress 
{
    private String ipAddressName;
    private String macAddress;


    public IpAddress(String ipAddressName, String macAddress) 
    {
        setIpAddressName(ipAddressName);
        setMacAddress(macAddress);
    }

    public void setIpAddressName(String ipAddressName) 
    {
        this.ipAddressName = ipAddressName;
    }

    public String getIpAddressName() 
    {
        return this.ipAddressName;
    }


    public void setMacAddress(String macAddress) 
    {
        this.macAddress = macAddress;
    }


    public String getMacAddress() 
    {
        return this.macAddress;
    }
}

But for performing these networking operation on main thread is not good. You have to push all code on thread. and perform all network related isses on background thread. Hope it helps you. let me know weather it works or not. If you need I would like to share .apk of this demo.

Mangesh Sambare
  • 594
  • 3
  • 23
  • you can have the apk file or the project? if not annoys . I did not understand the things – Edoardo Goffredo Mar 29 '16 at 10:07
  • i have both demo project and apk file also. but what did you not understand? – Mangesh Sambare Mar 29 '16 at 11:15
  • you please let pass me apk and project? I'd understand better .. it is not a problem .. What I do not understand is how to put all the IP andresses found in a list view – Edoardo Goffredo Mar 29 '16 at 13:16
  • could you please give your email address? – Mangesh Sambare Mar 30 '16 at 07:40
  • developer@copyworld.it – Edoardo Goffredo Mar 30 '16 at 12:44
  • hey @MangeshSambare do you still have apk? if possible can you send me @ haris.576@gmail.com. I'm getting result buts its little different then fing. – Haris Aug 22 '19 at 05:30
  • @Haris Results of fing & above code is different because fing app uses `NSD` and another network discovery mechanisms with ping mechanism. So that fing app gets proper mac address, ip address, network services running in those mac and also gets open network ports list. If you want like this then you have do research on this. You can read `zero-config` mechanism – Mangesh Sambare Aug 22 '19 at 07:21
0

You can try some like this:

public class MainActivity extends Activity  {

       WifiManager wifi;
       String wifis[];
       WifiScanReceiver wifiReciever;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
          wifiReciever = new WifiScanReceiver();
          wifi.startScan();
       }



       protected void onResume() {
          registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
          super.onResume();
       }



       private class WifiScanReceiver extends BroadcastReceiver{
          public void onReceive(Context c, Intent intent) {
             List<ScanResult> wifiScanList = wifi.getScanResults();
             wifis = new String[wifiScanList.size()];

             for(int i = 0; i < wifiScanList.size(); i++){
                wifis[i] = ((wifiScanList.get(i)).toString());
             }
             //here you can set your list view with wifis
          }
       }
    }
Kristiyan Varbanov
  • 2,439
  • 2
  • 17
  • 37