2

I want to get the ip address of my pc in android emulator through code....or tell me to achive ip address of all devices connected in a lan to identify each one uniquely .......please help me to sort out this problem

Thanks in advance

Ashish Tamrakar
  • 810
  • 10
  • 24

2 Answers2

2

use this code fetch the external ip address

      HttpClient httpclient = new DefaultHttpClient();
      HttpGet httpget = new HttpGet("http://api.externalip.net/ip/"); 
      HttpResponse response = null;
      try 
      {
      response = httpclient.execute(httpget);
        } 
      catch (ClientProtocolException e)
      {
     e.printStackTrace();
        } 
      catch (IOException e)
      {
     e.printStackTrace();
        }
      Log.e("",""+response);
      HttpEntity entity = response.getEntity();
      if (entity != null) {
      long len = entity.getContentLength();
      if (len != -1 && len < 1024) 
      {
       try
       {
      str=EntityUtils.toString(entity);
       Log.e("",""+str);
        }
       catch (ParseException e)
       {            
    e.printStackTrace();
    } 
       catch (IOException e)
       {                
    e.printStackTrace();
    }
    } 
      }
Nitesh Khosla
  • 875
  • 8
  • 20
2

The above functions are possible only by checking the arp cache where the IP address will be added one by one depending on how each one connect to the device. USe the below code and check. Just put button with proper name and call this method on click

    public void getClientList() {

    int macCount = 0;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");

            if (splitted != null && splitted.length >= 4) {
                // Basic sanity check
                String mac = splitted[3];

                if (mac.matches("..:..:..:..:..:..")) {
                    macCount++;
                    ClientList.add("Client(" + macCount + ")");
                    IpAddr.add(splitted[0]);
                    HWAddr.add(splitted[3]);
                    Device.add(splitted[5]);
                    Toast.makeText(
                            getApplicationContext(),
                            "Mac_Count  " + macCount + "   MAC_ADDRESS  "
                                    + mac, Toast.LENGTH_SHORT).show();
                    for (int i = 0; i < splitted.length; i++)
                        System.out.println("Addressssssss     "
                                + splitted[i]);
                }
            }
        }
        // ClientList.remove(0);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Shyam Dev
  • 126
  • 2
  • 10
  • could you explore this more if i want to identify the ip camera on network with this code....how can i do that – Ashish Tamrakar Apr 13 '12 at 06:44
  • @AshishTamrakar - Hi friend, IP camera (like Linksys) got the capability of wifi (like other devices) so that you can directly connect the camera to Android through wifi. In such case you can use IpAddr.add(splitted[0]); from the above code to fetch the IP address of the camera. But if the camera being operated by another server (like computer with its own software) then you need to go for socket programing so as to get the response from the server to get the Ipaddress of all devices(including camera) connected to that server using your android. – Shyam Dev Apr 16 '12 at 06:36