Example usage for android.net.wifi WifiInfo getMacAddress

List of usage examples for android.net.wifi WifiInfo getMacAddress

Introduction

In this page you can find the example usage for android.net.wifi WifiInfo getMacAddress.

Prototype

public String getMacAddress() 

Source Link

Usage

From source file:Main.java

public static String getMacAddress(Context context) {
    if (TextUtils.isEmpty(macAddress)) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo != null) {
            macAddress = wifiInfo.getMacAddress();
            return macAddress;
        }//from  w  ww. j  a  v a  2  s  .  co  m
    }
    return macAddress;
}

From source file:Main.java

public static String GetMacAddress(Context context) {
    String macAddress = null;//from   ww  w  .jav a2s  .  c om
    WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
    if (info != null) {
        macAddress = info.getMacAddress();
    }
    return macAddress;
}

From source file:Main.java

public static String getMacInfo(Context context) {
    String macAddress = null, ip = null;
    WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
    if (null != info) {
        macAddress = info.getMacAddress();

    }/*from  w  w w .java 2  s  .  c o m*/
    return macAddress;
}

From source file:Main.java

public static String getWifiMac(Context context) {
    String macAddr;// www.  j  a  va 2  s.com
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    macAddr = wifiInfo.getMacAddress();
    if (TextUtils.isEmpty(macAddr)) {
        NetworkInterface networkInterface = null;
        try {
            networkInterface = NetworkInterface.getByInetAddress(InetAddress.getByName(getIPAddr()));
            byte[] hardwareAddress = networkInterface.getHardwareAddress();
            macAddr = byte2hex(hardwareAddress);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
    return macAddr;
}

From source file:Main.java

/**
 * Returns MAC address of WiFi module./* w w  w.  j ava  2s  .  c  om*/
 *
 * @param context - used to get WiFi manager
 */
public static String getMacAddress(Context context) {
    final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    if (wifiInfo == null)
        return null;

    return wifiInfo.getMacAddress().toUpperCase();
}

From source file:Main.java

@SuppressLint("HardwareIds")
static String getPhoneMacAddress(Context context) {
    String mac = "";

    InputStreamReader inputStreamReader = null;
    LineNumberReader lineNumberReader = null;
    try {//from  w  w w .  j  a v  a  2 s.c om
        @SuppressLint("WifiManagerPotentialLeak")
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = manager.getConnectionInfo();
        mac = info.getMacAddress();

        if (TextUtils.isEmpty(mac)) {
            Process process = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
            inputStreamReader = new InputStreamReader(process.getInputStream());
            lineNumberReader = new LineNumberReader(inputStreamReader);
            String line = lineNumberReader.readLine();
            if (line != null) {
                mac = line.trim();
            }
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (inputStreamReader != null) {
            try {
                inputStreamReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (lineNumberReader != null) {
            try {
                lineNumberReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    if (TextUtils.isEmpty(mac)) {
        mac = "na";
    }

    return mac;
}

From source file:Main.java

static public String getMacAddress(Context context, ConnectivityManager connMananger) {
    Log.d(TAG, "getMacAddress");

    String macAddress = "";

    NetworkInfo info = connMananger.getActiveNetworkInfo();
    if (info != null && info.isConnected()) {
        int type = info.getType();

        Log.d(TAG, "connected type = " + type + " name " + info.getTypeName());

        if (type == ConnectivityManager.TYPE_WIFI) {
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            macAddress = wifiInfo.getMacAddress();
        } else if (type == ConnectivityManager.TYPE_ETHERNET || type == ConnectivityManager.TYPE_MOBILE) {
            String addressFileName = "/sys/class/net/eth0/address";
            File addressFile = new File(addressFileName);
            Log.d(TAG, "1");

            if (addressFile.exists()) {
                Log.d(TAG, "2");
                macAddress = readString(addressFile);
                Log.d(TAG, macAddress);//from  ww  w.j av a  2 s  .c om
            } else {
                addressFileName = "/sys/class/net/eth1/address";
                addressFile = new File(addressFileName);
                if (addressFile.exists()) {
                    macAddress = readString(addressFile);
                    Log.d(TAG, macAddress);
                }
            }
        } else {
            //other type;
        }
    }
    return macAddress;
}

From source file:Main.java

public static String getWifiAddress(Context pContext) {
    String address = DEFAULT_WIFI_ADDRESS;
    if (pContext != null) {
        WifiInfo localWifiInfo = ((WifiManager) pContext.getSystemService("wifi")).getConnectionInfo();
        if (localWifiInfo != null) {
            address = localWifiInfo.getMacAddress();
            if (address == null || address.trim().equals(""))
                address = DEFAULT_WIFI_ADDRESS;
            return address;
        }//from w ww .j  av  a  2s .  c om

    }
    return DEFAULT_WIFI_ADDRESS;
}

From source file:Main.java

/**
 * @Thach @SK29/*w w  w.  ja  va  2 s .  c  o m*/
 * @Init MAC address and save to preference
 * @param context
 * @return
 */
private static String initMacAddress(Context context) {
    // INIT macAddress
    String macAddress;

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    if (wifiManager.isWifiEnabled()) {
        // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

    } else {
        // ENABLE THE WIFI FIRST
        wifiManager.setWifiEnabled(true);

        // WIFI IS NOW ENABLED. GRAB THE MAC ADDRESS HERE
        WifiInfo info = wifiManager.getConnectionInfo();
        macAddress = info.getMacAddress();

        // NOW DISABLE IT AGAIN
        wifiManager.setWifiEnabled(false);
    }

    if (macAddress == null) {
        macAddress = android.os.Build.ID;
    }

    return macAddress;
}

From source file:com.vinexs.tool.NetworkManager.java

public static String getMacAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    return wInfo.getMacAddress();
}