get Client Mac From Arp Cache - Android java.net

Android examples for java.net:Mac Address

Description

get Client Mac From Arp Cache

Demo Code

import android.util.Log;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Main{


    private static final String TAG = "WifiUtils";
    public static String getClientMacFromArpCache() {
        BufferedReader br = null;
        try {/*from   w  ww  .  ja v a2 s  .c  om*/
            br = new BufferedReader(new FileReader("/proc/net/arp"));
            String line;
            Log.i(TAG, "line");
            if ((line = br.readLine()) != null) {
                line = br.readLine();
                Log.i(TAG, line);
                String[] splitted = line.split(" +");
                if (splitted != null && splitted.length >= 4) {
                    // Basic sanity check
                    String mac = splitted[3];
                    Log.i(TAG, mac);
                    if (mac.matches("..:..:..:..:..:..")) {
                        return mac;
                    } else {
                        return null;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}

Related Tutorials