Skip to content Skip to sidebar Skip to footer

How To Get Ip Address And Names Of All Devices In Local Network On Android

I want to see all the connected devices on my network with java, but I can't get it working. I have attached some screenshots below of how I want it to be output. I would like to h

Solution 1:

The main problem is that you're grabbing the wrong IP address. InetAddress.getLocalHost() is returning 127.0.0.1 and that's just your device.

Use the Wifi IP address in instead:

ConnectivityManagercm= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfoactiveNetwork= cm.getActiveNetworkInfo();
WifiManagerwm= (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

WifiInfoconnectionInfo= wm.getConnectionInfo();
intipAddress= connectionInfo.getIpAddress();
StringipString= Formatter.formatIpAddress(ipAddress);

Here is a quick and dirty AsyncTask that does that:

staticclassNetworkSniffTaskextendsAsyncTask<Void, Void, Void> {

  privatestaticfinalStringTAG= Constants.TAG + "nstask";

  private WeakReference<Context> mContextRef;

  publicNetworkSniffTask(Context context) {
    mContextRef = newWeakReference<Context>(context);
  }

  @Overrideprotected Void doInBackground(Void... voids) {
    Log.d(TAG, "Let's sniff the network");

    try {
      Contextcontext= mContextRef.get();

      if (context != null) {

        ConnectivityManagercm= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfoactiveNetwork= cm.getActiveNetworkInfo();
        WifiManagerwm= (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

        WifiInfoconnectionInfo= wm.getConnectionInfo();
        intipAddress= connectionInfo.getIpAddress();
        StringipString= Formatter.formatIpAddress(ipAddress);


        Log.d(TAG, "activeNetwork: " + String.valueOf(activeNetwork));
        Log.d(TAG, "ipString: " + String.valueOf(ipString));

        Stringprefix= ipString.substring(0, ipString.lastIndexOf(".") + 1);
        Log.d(TAG, "prefix: " + prefix);

        for (inti=0; i < 255; i++) {
          StringtestIp= prefix + String.valueOf(i);

          InetAddressaddress= InetAddress.getByName(testIp);
          booleanreachable= address.isReachable(1000);
          StringhostName= address.getCanonicalHostName();

          if (reachable)
            Log.i(TAG, "Host: " + String.valueOf(hostName) + "(" + String.valueOf(testIp) + ") is reachable!");
        }
      }
    } catch (Throwable t) {
      Log.e(TAG, "Well that's not good.", t);
    }

  returnnull;
}

Here are the permissions:

<uses-permissionandroid:name="android.permission.INTERNET" /><uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE" /><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />

Not all routers allow this, so to get the names in a other way is to send the mac adress to an api and get the brand name back in return.

StringmacAdress="5caafd1b0019";
StringdataUrl="http://api.macvendors.com/" + macAdress;
HttpURLConnectionconnection=null;
try {
    URLurl=newURL(dataUrl);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    DataOutputStreamwr=newDataOutputStream(connection.getOutputStream());
    wr.flush();
    wr.close();
    InputStreamis= connection.getInputStream();
    BufferedReaderrd=newBufferedReader(newInputStreamReader(is));
    StringBufferresponse=newStringBuffer();
    String line;
    while ((line = rd.readLine()) != null) {response.append(line);response.append('\r');}
    rd.close();
    StringresponseStr= response.toString();
    Log.d("Server response", responseStr);
} catch (Exception e) {e.printStackTrace();} finally {if (connection != null) {connection.disconnect();}}

Post a Comment for "How To Get Ip Address And Names Of All Devices In Local Network On Android"