Scan Devices Ip Connected Inwards Wifi Network

Posted on

Example to scan connected devices inside the same WiFi network, using InetAddress.isReachable().

package com.blogspot.android_er.androidwireless;  import android.content.Context; import android.net.wifi.WifiManager; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.TextView;  import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList;  world course of report MainActivity extends AppCompatActivity {      TextView tvWifiState;     TextView tvScanning, tvResult;      ArrayList<InetAddress> inetAddresses;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          tvWifiState = (TextView)findViewById(R.id.WifiState);         tvScanning = (TextView)findViewById(R.id.Scanning);         tvResult = (TextView)findViewById(R.id.Result);          //To foreclose retention leaks on devices prior to Android N,         //retrieve WifiManager amongst         //getApplicationContext().getSystemService(Context.WIFI_SERVICE),         //instead of getSystemService(Context.WIFI_SERVICE)         WifiManager wifiManager =                 (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);         tvWifiState.setText(readtvWifiState(wifiManager));          novel ScanTask(tvScanning, tvResult).execute();     }      // "android.permission.ACCESS_WIFI_STATE" is needed     person String readtvWifiState(WifiManager wm){         String trial = "";         switch (wm.getWifiState()){             illustration WifiManager.WIFI_STATE_DISABLED:                 trial = "WIFI_STATE_DISABLED";                 break;             illustration WifiManager.WIFI_STATE_DISABLING:                 trial = "WIFI_STATE_DISABLING";                 break;             illustration WifiManager.WIFI_STATE_ENABLED:                 trial = "WIFI_STATE_ENABLED";                 break;             illustration WifiManager.WIFI_STATE_ENABLING:                 trial = "WIFI_STATE_ENABLING";                 break;             illustration WifiManager.WIFI_STATE_UNKNOWN:                 trial = "WIFI_STATE_UNKNOWN";                 break;             default:         }         supply result;     }      person course of report ScanTask extends AsyncTask<Void, String, Void> {          TextView tvCurrentScanning, tvScanResullt;         ArrayList<String> canonicalHostNames;          world ScanTask(TextView tvCurrentScanning, TextView tvScanResullt) {             this.tvCurrentScanning = tvCurrentScanning;             this.tvScanResullt = tvScanResullt;         }          @Override         protected void onPostExecute(Void aVoid) {             tvCurrentScanning.setText("Finished.");             tvScanResullt.setText("");             for(int i = 0; i < inetAddresses.size(); i++){                 tvScanResullt.append(canonicalHostNames.get(i) + "\n");             }         }          @Override         protected Void doInBackground(Void... voids) {             scanInetAddresses();             supply null;         }          @Override         protected void onProgressUpdate(String... values) {             tvCurrentScanning.setText(values[0]);         }          person void scanInetAddresses(){             //May hold upward you lot convey to arrange the timeout             concluding int timeout = 500;              if(inetAddresses == null){                 inetAddresses = novel ArrayList<>();             }             inetAddresses.clear();              if(canonicalHostNames == null){                 canonicalHostNames = novel ArrayList<>();             }             canonicalHostNames.clear();              //For demonstration, scan 192.168.1.xxx solely             byte[] ip = {(byte) 192, (byte) 168, (byte) 1, 0};             for (int j = 0; j < 255; j++) {                 ip[3] = (byte) j;                 endeavor {                     InetAddress checkAddress = InetAddress.getByAddress(ip);                     publishProgress(checkAddress.getCanonicalHostName());                     if (checkAddress.isReachable(timeout)) {                         inetAddresses.add(checkAddress);                         canonicalHostNames.add(checkAddress.getCanonicalHostName());                     }                 } grab (UnknownHostException e) {                     e.printStackTrace();                     publishProgress(e.getMessage());                 } grab (IOException e) {                     e.printStackTrace();                     publishProgress(e.getMessage());                 }             }         }     } }  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidwireless.MainActivity" >      <TextView         android:id="@+id/title"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <TextView         android:id="@+id/WifiState"         android:layout_width="match_parent"         android:layout_height="wrap_content" />      <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:orientation="horizontal">         <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="Scanning: "/>         <TextView             android:id="@+id/Scanning"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:textStyle="italic"/>     </LinearLayout>      <TextView         android:id="@+id/Result"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textStyle="bold"/>  </LinearLayout>  

uses-permission of “android.permission.ACCESS_WIFI_STATE” in addition to “android.permission.INTERNET” are needed.