Display WiFi Hotspot clients by cat proc net arp

This example display WiFi Hotspot connected clients (with IP and MAC address) by Linux command "cat /proc/net/arp", using ProcessBuilder.

(You can download runnable APK from link on bottom)

Tethering OFF, as WiFi client.

Tethering ON, with no client connected.

Tethering ON, with two clients connected; Nexus 7 and Raspberry Pi + WiFi dongle.

But, after any client disconnected, the /proc/net/arp will not updated immediately; I dont know how long it will refresh. One way to update arp is turn OFF and ON tethering again.

Tested on unrooted Xiaomi Redmi 2 running Android 4.4.4.

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.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

Button btnRead;
TextView textResult;

//String[] args = {"/system/bin/cat", "/proc/net/arp"};
String[] args = {"cat", "/proc/net/arp"};

@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);

btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textResult.setText(toRead());
}
});
}

private String toRead()
{
ProcessBuilder cmd;
String result="";

try{
cmd = new ProcessBuilder(args);

Process process = cmd.start();
InputStream in = process.getInputStream();
byte[] re = new byte[1024];
while(in.read(re) != -1){
System.out.println(new String(re));
result = result + new String(re);
}
in.close();
} catch(IOException ex){
ex.printStackTrace();
}
return result;
}
}


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 /proc/net/arp"/>

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


download filesDownload runnable APK .

This example display arp in human readable format, but not for machine. Next post "Retrieve IP and MAC addresses from /proc/net/arp" get it in more machine readable.

Related Posts by Categories

0 comments:

Post a Comment

Blog Archive

Powered by Blogger.