zoukankan      html  css  js  c++  java
  • 获取Mac地址

    public class DeviceMacUtil {
    
        /**
         * Android  6.0 之前(不包括6.0)
         * 必须的权限  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
         * @param context
         * @return
         */
        private static String getMacDefault(Context context) {
            String mac = null;
            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 String getMacAddress() {
            String WifiAddress =  null;
            try {
                WifiAddress = new BufferedReader(new FileReader(new File("/sys/class/net/wlan0/address"))).readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return WifiAddress;
        }
    
        /**
         * 遍历循环所有的网络接口,找到接口是 wlan0
         * 必须的权限 <uses-permission android:name="android.permission.INTERNET" />
         * @return
         */
        private static String getMacFromHardware() {
            try {
                List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
                Log.d("Utils", "all:" + all.size());
                for (NetworkInterface nif : all) {
                    if (!nif.getName().equalsIgnoreCase("wlan0")){
                        continue;
                    }
    
                    byte[] macBytes = nif.getHardwareAddress();
                    if (macBytes == null) {
                        return null;
                    }
                    Log.d("Utils", "macBytes:" + macBytes.length + "," + nif.getName());
    
                    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 null;
        }
    
        public static String getMacFromHardware(Context context) {
    
            String macAddress;
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
                macAddress = getMacDefault(context);
                if (macAddress != null ) {
                    Log.d("Utils", "android 5.0以前的方式获取mac"+macAddress);
                    macAddress =  macAddress.replaceAll(":","");
                    if(macAddress.equalsIgnoreCase("020000000000")== false){
                        return macAddress;
                    }
                }
            }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
                macAddress = getMacAddress();
                if (macAddress != null ) {
                    Log.d("Utils", "android 6~7 的方式获取的mac"+macAddress);
                    macAddress =  macAddress.replaceAll(":","");
                    if(macAddress.equalsIgnoreCase("020000000000")== false){
                        return macAddress;
                    }
                }
            }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                macAddress = getMacFromHardware();
                if (macAddress != null ) {
                    Log.d("Utils", "android 7以后 的方式获取的mac"+macAddress);
                    macAddress =  macAddress.replaceAll(":","");
                    if(macAddress.equalsIgnoreCase("020000000000") == false){
                        return macAddress;
                    }
                }
            }
    
            Log.d("Utils", "没有获取到MAC");
            return null;
        }
  • 相关阅读:
    jmeter中生成不重复的序列
    jmeter返回解密
    jmeter请求加密
    查看链接设备和安装软件
    jmeter_事物控制器_Transaction Controller
    jmeter返回中提取匹配多个参数($1$$2$)
    jmeter官方文档地址
    DEDECMS会员注册如何配置邮箱发送邮件功能
    颤抖吧,Css3
    html embed用法
  • 原文地址:https://www.cnblogs.com/matd/p/12855869.html
Copyright © 2011-2022 走看看