android获取mac地址

2019-02-14  本文已影响2人  青色石头ing

获取mac地址分为三段

1.Android 6.0 之前(不包括6.0)

2.Android 6.0(包括) - Android 7.0(不包括)

3.Android7.0(包括)之后

获取方法

/**

* Android  6.0 之前(不包括6.0)

* 必须的权限 

* @param context

* @return

*/

private static StringgetMacDefault(Context context) {

String mac ="02:00:00:00:00:00";

  if (context ==null) {

return mac;

  }

WifiManager wifi = (WifiManager) context.getApplicationContext()

.getSystemService(Context.WIFI_SERVICE);

  if (wifi ==null) {

return mac;

  }

WifiInfo info =null;

  try {

info = wifi.getConnectionInfo();

  }catch (Exception e) {

}

if (info ==null) {

return null;

  }

mac = info.getMacAddress();

  if (!TextUtils.isEmpty(mac)) {

mac = mac.toUpperCase(Locale.ENGLISH);

  }

return mac;

}

/**

* Android 6.0(包括) - Android 7.0(不包括)

* @return

*/

private static StringgetMacFromFile() {

String str ="";

  String macSerial ="";

  try {

Process pp = Runtime.getRuntime().exec(

"cat /sys/class/net/wlan0/address");

      InputStreamReader ir =new InputStreamReader(pp.getInputStream());

      LineNumberReader input =new LineNumberReader(ir);

      for (; null != str; ) {

str = input.readLine();

        if (str !=null) {

macSerial = str.trim();// 去空格

            break;

        }

}

}catch (Exception ex) {

ex.printStackTrace();

  }

if (macSerial ==null ||"".equals(macSerial)) {

try {

return loadFileAsString("/sys/class/net/eth0/address")

.toUpperCase().substring(0, 17);

      }catch (Exception e) {

e.printStackTrace();

      }

}

return macSerial;

}

private static StringloadFileAsString(String fileName)throws Exception {

FileReader reader =new FileReader(fileName);

  String text =loadReaderAsString(reader);

  reader.close();

  return text;

}

private static StringloadReaderAsString(Reader reader)throws Exception {

StringBuilder builder =new StringBuilder();

  char[] buffer =new char[4096];

  int readLength = reader.read(buffer);

  while (readLength >=0) {

builder.append(buffer, 0, readLength);

      readLength = reader.read(buffer);

  }

return builder.toString();

}

/**7.0之后

* 遍历循环所有的网络接口,找到接口是 wlan0

* 必须的权限

* @return

*/

private static StringgetMacFromHardware() {

try {

List all = Collections.list(NetworkInterface.getNetworkInterfaces());

      for (NetworkInterface nif : all) {

if (!nif.getName().equalsIgnoreCase("wlan0"))continue;

        byte[] macBytes = nif.getHardwareAddress();

        if (macBytes ==null) {

return "";

        }

StringBuilder res1 =new StringBuilder();

        for (byte b : macBytes) {

res1.append(String.format("%02X:", b));

        }

if (res1.length() >0) {

res1.deleteCharAt(res1.length() -1);

        }

return res1.toString();

      }

}catch (Exception e) {

e.printStackTrace();

  }

return "02:00:00:00:00:00";

}

调用方法

/**

* 获取mac地址

*

* @return

*/

private static StringgetMacAddress(Context context) {

String mac ="02:00:00:00:00:00";

  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {

mac =getMacDefault(context);

  }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {

mac =getMacFromFile();

  }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

mac =getMacFromHardware();

  }

return mac;

}

上一篇 下一篇

猜你喜欢

热点阅读