Java tutorial
//package com.java2s; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.text.TextUtils; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class Main { public static String getWifiMac(Context context) { String macAddr; 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; } public static String getIPAddr() { try { Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress inetAddress = inetAddresses.nextElement(); return inetAddress.getHostAddress().toString(); } } } catch (SocketException e) { e.printStackTrace(); } return null; } public static String byte2hex(byte[] b) { StringBuffer hs = new StringBuffer(b.length); String stmp = ""; int len = b.length; for (int n = 0; n < len; n++) { stmp = Integer.toHexString(b[n] & 0xFF); if (stmp.length() == 1) hs = hs.append("0").append(stmp); else { hs = hs.append(stmp); } } return String.valueOf(hs); } }