MacAndIDUtil.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.xzjmyk.pm.activity.util;
  2. import android.content.Context;
  3. import android.net.wifi.WifiInfo;
  4. import android.net.wifi.WifiManager;
  5. import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil;
  6. import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
  7. import java.io.FileInputStream;
  8. /**
  9. * Created by pengminggong on 2016/10/31.
  10. */
  11. public class MacAndIDUtil {
  12. public String getMac(Context ct) {
  13. String mac = null;
  14. String macOld = CommonUtil.getSharedPreferences(ct, "macaddress");
  15. if (StringUtils.isEmpty(macOld)) {
  16. mac = getLocalMacAddress();
  17. if (StringUtils.isEmpty(mac))
  18. mac = getMacFromWifiInfo(ct);
  19. boolean isOk = legitimateMac(mac);//判断mac地址是否合法
  20. if (isOk) {
  21. CommonUtil.setSharedPreferences(ct, "macaddress", mac);
  22. } else {
  23. tryOpenWifi(ct);
  24. mac = "";
  25. }
  26. } else {
  27. mac = macOld;
  28. }
  29. return mac;
  30. }
  31. private boolean legitimateMac(String mac) {
  32. return !StringUtils.isEmpty(mac) && !mac.contains("00:00:00");
  33. }
  34. /*通过wifiInfo获取ip*/
  35. private String getIpFromWifiInfo(Context ct) {
  36. WifiInfo info = getWifiInfo(ct);
  37. if (null != info)
  38. return int2ip(info.getIpAddress());
  39. else
  40. return null;
  41. }
  42. /*通过wifiInfo获取mac*/
  43. private String getMacFromWifiInfo(Context ct) {
  44. //在wifi未开启状态下,仍然可以获取MAC地址,但是IP地址必须在已连接状态下否则为0
  45. WifiInfo info = getWifiInfo(ct);
  46. if (null != info) {
  47. return info.getMacAddress();
  48. } else
  49. return null;
  50. }
  51. /*获取wifiInfo*/
  52. private WifiInfo getWifiInfo(Context ct) {
  53. WifiManager wifiMgr = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE);
  54. if (wifiMgr == null) {
  55. return null;
  56. }
  57. return wifiMgr.getConnectionInfo();
  58. }
  59. /*将地址整形转化为字符串*/
  60. public static String int2ip(long ipInt) {
  61. StringBuilder sb = new StringBuilder();
  62. sb.append(ipInt & 0xFF).append(".");
  63. sb.append((ipInt >> 8) & 0xFF).append(".");
  64. sb.append((ipInt >> 16) & 0xFF).append(".");
  65. sb.append((ipInt >> 24) & 0xFF);
  66. return sb.toString();
  67. }
  68. //获取
  69. private String getLocalMacAddress() {
  70. String mac = null;
  71. try {
  72. String path = "sys/class/net/eth0/address";
  73. FileInputStream fis_name = new FileInputStream(path);
  74. byte[] buffer_name = new byte[8192];
  75. int byteCount_name = fis_name.read(buffer_name);
  76. if (byteCount_name > 0) {
  77. mac = new String(buffer_name, 0, byteCount_name, "utf-8");
  78. }
  79. if (mac == null) {
  80. fis_name.close();
  81. return "";
  82. }
  83. fis_name.close();
  84. } catch (Exception io) {
  85. String path = "sys/class/net/wlan0/address";
  86. FileInputStream fis_name;
  87. try {
  88. fis_name = new FileInputStream(path);
  89. byte[] buffer_name = new byte[8192];
  90. int byteCount_name = fis_name.read(buffer_name);
  91. if (byteCount_name > 0) {
  92. mac = new String(buffer_name, 0, byteCount_name, "utf-8");
  93. }
  94. fis_name.close();
  95. } catch (Exception e) {
  96. // TODO Auto-generated catch block
  97. e.printStackTrace();
  98. }
  99. }
  100. if (mac == null) {
  101. return "";
  102. } else {
  103. return mac.trim();
  104. }
  105. }
  106. private boolean tryOpenWifi(Context ct) {
  107. boolean softOpenWifi = false;
  108. WifiManager manager = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE);
  109. int state = manager.getWifiState();
  110. if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING) {
  111. manager.setWifiEnabled(true);
  112. softOpenWifi = true;
  113. }
  114. return softOpenWifi;
  115. }
  116. }