package com.xzjmyk.pm.activity.util; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil; import com.xzjmyk.pm.activity.ui.erp.util.StringUtils; import java.io.FileInputStream; /** * Created by pengminggong on 2016/10/31. */ public class MacAndIDUtil { public String getMac(Context ct) { String mac = null; String macOld = CommonUtil.getSharedPreferences(ct, "macaddress"); if (StringUtils.isEmpty(macOld)) { mac = getLocalMacAddress(); if (StringUtils.isEmpty(mac)) mac = getMacFromWifiInfo(ct); boolean isOk = legitimateMac(mac);//判断mac地址是否合法 if (isOk) { CommonUtil.setSharedPreferences(ct, "macaddress", mac); } else { tryOpenWifi(ct); mac = ""; } } else { mac = macOld; } return mac; } private boolean legitimateMac(String mac) { return !StringUtils.isEmpty(mac) && !mac.contains("00:00:00"); } /*通过wifiInfo获取ip*/ private String getIpFromWifiInfo(Context ct) { WifiInfo info = getWifiInfo(ct); if (null != info) return int2ip(info.getIpAddress()); else return null; } /*通过wifiInfo获取mac*/ private String getMacFromWifiInfo(Context ct) { //在wifi未开启状态下,仍然可以获取MAC地址,但是IP地址必须在已连接状态下否则为0 WifiInfo info = getWifiInfo(ct); if (null != info) { return info.getMacAddress(); } else return null; } /*获取wifiInfo*/ private WifiInfo getWifiInfo(Context ct) { WifiManager wifiMgr = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE); if (wifiMgr == null) { return null; } return wifiMgr.getConnectionInfo(); } /*将地址整形转化为字符串*/ public static String int2ip(long ipInt) { StringBuilder sb = new StringBuilder(); sb.append(ipInt & 0xFF).append("."); sb.append((ipInt >> 8) & 0xFF).append("."); sb.append((ipInt >> 16) & 0xFF).append("."); sb.append((ipInt >> 24) & 0xFF); return sb.toString(); } //获取 private String getLocalMacAddress() { String mac = null; try { String path = "sys/class/net/eth0/address"; FileInputStream fis_name = new FileInputStream(path); byte[] buffer_name = new byte[8192]; int byteCount_name = fis_name.read(buffer_name); if (byteCount_name > 0) { mac = new String(buffer_name, 0, byteCount_name, "utf-8"); } if (mac == null) { fis_name.close(); return ""; } fis_name.close(); } catch (Exception io) { String path = "sys/class/net/wlan0/address"; FileInputStream fis_name; try { fis_name = new FileInputStream(path); byte[] buffer_name = new byte[8192]; int byteCount_name = fis_name.read(buffer_name); if (byteCount_name > 0) { mac = new String(buffer_name, 0, byteCount_name, "utf-8"); } fis_name.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (mac == null) { return ""; } else { return mac.trim(); } } private boolean tryOpenWifi(Context ct) { boolean softOpenWifi = false; WifiManager manager = (WifiManager) ct.getSystemService(Context.WIFI_SERVICE); int state = manager.getWifiState(); if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING) { manager.setWifiEnabled(true); softOpenWifi = true; } return softOpenWifi; } }