| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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;
- }
- }
|