Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.InputStream;

import android.content.Context;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class Main {
    public static String getLocalMacAddress(Context mc) {
        String defmac = "00:00:00:00:00:00";
        InputStream input = null;
        String wifimac = getWifiMacAddress(mc);
        if (null != wifimac) {
            if (!wifimac.equals(defmac))
                return wifimac;
        }
        try {

            ProcessBuilder builder = new ProcessBuilder("busybox", "ifconfig");
            Process process = builder.start();
            input = process.getInputStream();

            byte[] b = new byte[1024];
            StringBuffer buffer = new StringBuffer();
            while (input.read(b) > 0) {
                buffer.append(new String(b));
            }
            String value = buffer.substring(0);
            String systemFlag = "HWaddr ";
            int index = value.indexOf(systemFlag);
            // List <String> address = new ArrayList <String> ();
            if (0 < index) {
                value = buffer.substring(index + systemFlag.length());
                // address.add(value.substring(0,18));
                defmac = value.substring(0, 17);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defmac;
    }

    public static String getWifiMacAddress(Context mc) {
        WifiManager wifi = (WifiManager) mc.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }
}