Scan Reachable Ip To Notice Devices Inward Network

Posted on


This example, scan a make of IPs, to cheque if it is reachable, inwards plough to discovery connected devices inwards the same network.

MainActivity.java

package com.blogspot.android_er.androidipscanner;  import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.Toast;  import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList;  world course of education MainActivity extends AppCompatActivity {      somebody Button btnScan;     somebody ListView listViewIp;      ArrayList<String> ipList;     ArrayAdapter<String> adapter;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btnScan = (Button)findViewById(R.id.scan);         listViewIp = (ListView)findViewById(R.id.listviewip);           ipList = novel ArrayList();         adapter = novel ArrayAdapter<String>(this,                 android.R.layout.simple_list_item_1, android.R.id.text1, ipList);         listViewIp.setAdapter(adapter);          btnScan.setOnClickListener(new View.OnClickListener() {             @Override             world void onClick(View v) {                 novel ScanIpTask().execute();             }         });      }      somebody course of education ScanIpTask extends AsyncTask<Void, String, Void>{          /*         Scan IP 192.168.1.100 192.168.1.110         you lot should endeavor dissimilar timeout for your network/devices          */         static terminal String subnet = "192.168.1.";         static terminal int lower = 100;         static terminal int upper = 110;         static terminal int timeout = 5000;          @Override         protected void onPreExecute() {             ipList.clear();             adapter.notifyDataSetInvalidated();             Toast.makeText(MainActivity.this, "Scan IP...", Toast.LENGTH_LONG).show();         }          @Override         protected Void doInBackground(Void... params) {              for (int i = lower; i <= upper; i++) {                 String host = subnet + i;                  endeavor {                     InetAddress inetAddress = InetAddress.getByName(host);                     if (inetAddress.isReachable(timeout)){                         publishProgress(inetAddress.toString());                     }                  } grab (UnknownHostException e) {                     e.printStackTrace();                 } grab (IOException e) {                     e.printStackTrace();                 }             }              provide null;         }          @Override         protected void onProgressUpdate(String... values) {             ipList.add(values[0]);             adapter.notifyDataSetInvalidated();             Toast.makeText(MainActivity.this, values[0], Toast.LENGTH_LONG).show();         }          @Override         protected void onPostExecute(Void aVoid) {             Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_LONG).show();         }     }  }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <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:padding="16dp"     android:orientation="vertical"     tools:context=".MainActivity">      <TextView         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" />      <Button         android:id="@+id/scan"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Scan"/>     <ListView         android:id="@+id/listviewip"         android:layout_width="match_parent"         android:layout_height="wrap_content"/> </LinearLayout>  

Permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml

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

Related:
Java version run on Raspberry Pi – scan connected IP inwards the same network