MacAndIDUtil.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. import java.net.NetworkInterface;
  9. import java.net.SocketException;
  10. import java.util.Enumeration;
  11. /**
  12. * Created by pengminggong on 2016/10/31.
  13. */
  14. public class MacAndIDUtil {
  15. public String getMac(Context ct) {
  16. String mac = null;
  17. String macOld = CommonUtil.getSharedPreferences(ct, "macaddress");
  18. if (macisEmpty(macOld)) {
  19. mac = getLocalMacAddress();
  20. if (macisEmpty(mac))
  21. mac = getMacFromWifiInfo(ct);
  22. if (macisEmpty(mac))
  23. mac = getLocalEthernetMacAddress();
  24. if (!macisEmpty(mac)) {
  25. CommonUtil.setSharedPreferences(ct, "macaddress", mac);
  26. } else {
  27. tryOpenWifi(ct);
  28. mac = "";
  29. }
  30. } else {
  31. mac = macOld;
  32. }
  33. return mac;
  34. }
  35. private boolean macisEmpty(String mac) {
  36. return StringUtils.isEmpty(mac) || mac.contains("00:00:00");
  37. }
  38. /*通过wifiInfo获取ip*/
  39. private String getIpFromWifiInfo(Context ct) {
  40. WifiInfo info = getWifiInfo(ct);
  41. if (null != info)
  42. return int2ip(info.getIpAddress());
  43. else
  44. return null;
  45. }
  46. /*通过wifiInfo获取mac*/
  47. private String getMacFromWifiInfo(Context ct) {
  48. //在wifi未开启状态下,仍然可以获取MAC地址,但是IP地址必须在已连接状态下否则为0
  49. WifiInfo info = getWifiInfo(ct);
  50. if (null != info) {
  51. return info.getMacAddress();
  52. } else
  53. return null;
  54. }
  55. /*获取wifiInfo*/
  56. private WifiInfo getWifiInfo(Context ct) {
  57. WifiManager wifiMgr = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE);
  58. if (wifiMgr == null) {
  59. return null;
  60. }
  61. return wifiMgr.getConnectionInfo();
  62. }
  63. /*将地址整形转化为字符串*/
  64. public String int2ip(long ipInt) {
  65. StringBuilder sb = new StringBuilder();
  66. sb.append(ipInt & 0xFF).append(".");
  67. sb.append((ipInt >> 8) & 0xFF).append(".");
  68. sb.append((ipInt >> 16) & 0xFF).append(".");
  69. sb.append((ipInt >> 24) & 0xFF);
  70. return sb.toString();
  71. }
  72. //获取
  73. private String getLocalMacAddress() {
  74. String mac = null;
  75. try {
  76. String path = "sys/class/net/eth0/address";
  77. FileInputStream fis_name = new FileInputStream(path);
  78. byte[] buffer_name = new byte[8192];
  79. int byteCount_name = fis_name.read(buffer_name);
  80. if (byteCount_name > 0) {
  81. mac = new String(buffer_name, 0, byteCount_name, "utf-8");
  82. }
  83. if (mac == null) {
  84. fis_name.close();
  85. return "";
  86. }
  87. fis_name.close();
  88. } catch (Exception io) {
  89. String path = "sys/class/net/wlan0/address";
  90. FileInputStream fis_name;
  91. try {
  92. fis_name = new FileInputStream(path);
  93. byte[] buffer_name = new byte[8192];
  94. int byteCount_name = fis_name.read(buffer_name);
  95. if (byteCount_name > 0) {
  96. mac = new String(buffer_name, 0, byteCount_name, "utf-8");
  97. }
  98. fis_name.close();
  99. } catch (Exception e) {
  100. // TODO Auto-generated catch block
  101. e.printStackTrace();
  102. }
  103. }
  104. if (mac == null) {
  105. return "";
  106. } else {
  107. return mac.trim();
  108. }
  109. }
  110. private boolean tryOpenWifi(Context ct) {
  111. boolean softOpenWifi = false;
  112. WifiManager manager = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE);
  113. int state = manager.getWifiState();
  114. if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING) {
  115. manager.setWifiEnabled(true);
  116. softOpenWifi = true;
  117. }
  118. return softOpenWifi;
  119. }
  120. public static String getLocalEthernetMacAddress() {
  121. String mac = null;
  122. try {
  123. Enumeration localEnumeration = NetworkInterface
  124. .getNetworkInterfaces();
  125. while (localEnumeration.hasMoreElements()) {
  126. NetworkInterface localNetworkInterface = (NetworkInterface) localEnumeration
  127. .nextElement();
  128. String interfaceName = localNetworkInterface.getDisplayName();
  129. if (interfaceName == null) {
  130. continue;
  131. }
  132. if (interfaceName.equals("eth0")) {
  133. mac = convertToMac(localNetworkInterface
  134. .getHardwareAddress());
  135. if (mac != null && mac.startsWith("0:")) {
  136. mac = "0" + mac;
  137. }
  138. break;
  139. }
  140. if (interfaceName.equals("wlan0")) {
  141. mac = convertToMac(localNetworkInterface
  142. .getHardwareAddress());
  143. if (mac != null && mac.startsWith("0:")) {
  144. mac = "0" + mac;
  145. }
  146. break;
  147. }
  148. }
  149. } catch (SocketException e) {
  150. e.printStackTrace();
  151. }
  152. return mac;
  153. }
  154. private static String convertToMac(byte[] mac) {
  155. StringBuilder sb = new StringBuilder();
  156. for (int i = 0; i < mac.length; i++) {
  157. byte b = mac[i];
  158. int value = 0;
  159. if (b >= 0 && b <= 16) {
  160. value = b;
  161. sb.append("0" + Integer.toHexString(value));
  162. } else if (b > 16) {
  163. value = b;
  164. sb.append(Integer.toHexString(value));
  165. } else {
  166. value = 256 + b;
  167. sb.append(Integer.toHexString(value));
  168. }
  169. if (i != mac.length - 1) {
  170. sb.append(":");
  171. }
  172. }
  173. return sb.toString();
  174. }
  175. }