Showing posts with label ip. Show all posts
Showing posts with label ip. Show all posts

Scan Reachable IP to discover devices in network

| 0 comments |

This example, scan a range of IPs, to check if it is reachable, in turn to discover connected devices in 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;

public class MainActivity extends AppCompatActivity {

private Button btnScan;
private 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 = new ArrayList();
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ipList);
listViewIp.setAdapter(adapter);

btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ScanIpTask().execute();
}
});

}

private class ScanIpTask extends AsyncTask<Void, String, Void>{

/*
Scan IP 192.168.1.100~192.168.1.110
you should try different timeout for your network/devices
*/
static final String subnet = "192.168.1.";
static final int lower = 100;
static final int upper = 110;
static final 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;

try {
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isReachable(timeout)){
publishProgress(inetAddress.toString());
}

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

return 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


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="http://android-er.blogspot.com/"
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 in AndroidManifest.xml
 <uses-permission android_name="android.permission.INTERNET"/>



Related:
- Java version run on Raspberry Pi - scan connected IP in the same network

Read More..

Retrieve IP and MAC addresses from proc net arp

| 0 comments |
Last post "Display WiFi Hotspot clients by "cat /proc/net/arp"" display arp as human readable string, but not machine readable. Its another version to retrieve IP and MAC addresses, store in a ArrayList.


MainActivity.java
package com.blogspot.android_er.androidlistclient;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Button btnRead;
TextView textResult;

ArrayList<Node> listNote;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRead = (Button)findViewById(R.id.readclient);
textResult = (TextView)findViewById(R.id.result);

listNote = new ArrayList<>();

btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
readAddresses();
textResult.setText("");
for(int i=0; i<listNote.size(); i++){
textResult.append(i + " ");
textResult.append(listNote.get(i).toString());
textResult.append(" ");
}
}
});
}

private void readAddresses() {
listNote.clear();
BufferedReader bufferedReader = null;

try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));

String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
Node thisNode = new Node(ip, mac);
listNote.add(thisNode);
}
}
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

class Node {
String ip;
String mac;

Node(String ip, String mac){
this.ip = ip;
this.mac = mac;
}

@Override
public String toString() {
return ip + " " + mac;
}
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context="com.blogspot.android_er.androidlistclient.MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<Button
android_id="@+id/readclient"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_textAllCaps="false"
android_text="Read Ip/MAC addresses"/>

<TextView
android_id="@+id/result"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_typeface="monospace"
android_textSize="24sp"/>
</LinearLayout>


Next:
- Lookup manufacturer info by MAC address, using www.macvendorlookup.com API
- Get HostName of WiFi hotspot clients, and check if it is still connected

Read More..