Showing posts with label Android code sample: ProcessBuilder. Show all posts
Showing posts with label Android code sample: ProcessBuilder. Show all posts

Sunday, December 20, 2015

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 don't 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 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="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.

Monday, February 10, 2014

Run Linux command with ProcessBuilder

This example show how to run Linux command in Android app with java.lang.ProcessBuilder.
remark: NOT all Linux commands supported.

Linux command in Android app
Linux command 'ls -s' run in Android app

package com.example.androidruncommand;

import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;
import android.widget.TextView;
import android.app.Activity;

public class MainActivity extends Activity {
 
 TextView prompt;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  prompt = (TextView)findViewById(R.id.prompt);
  
  //String[] command = {"df", "-h", "/"};
  //String[] command = {"df"};
  //String[] command = {"ls"};
  String[] command = {"ls", "-s"};
  
  StringBuilder cmdReturn = new StringBuilder();
  
  try {
   ProcessBuilder processBuilder = new ProcessBuilder(command);
   Process process = processBuilder.start();
   
   InputStream inputStream = process.getInputStream();
   int c;
            while ((c = inputStream.read()) != -1) {
                cmdReturn.append((char) c);
            }
            
            prompt.setText(cmdReturn.toString());
            
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    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" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/prompt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>