get Client Mac Address From Arp Cache - Android Wifi

Android examples for Wifi:Wifi Address

Description

get Client Mac Address From Arp Cache

Demo Code


//package com.java2s;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import android.util.Log;

public class Main {
    private static final String TAG = "WifiUtils";

    public static String getClientMacFromArpCache() {
        BufferedReader br = null;
        try {// w  w w.  j av  a2  s.  c o  m
            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