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;
        }
  • 相关阅读:
    零基础学习java------day1------计算机基础以及java的一些简单了解
    Mongodb的简单使用
    02-爬取http://www.allitebooks.org/网站,获取图片url,书名,简介,作者
    商业爬虫学习笔记day8-------json的使用
    商业爬虫学习笔记day7-------解析方法之bs4
    商业爬虫学习笔记day6
    练习1--爬取btc论坛的title和相应的url
    商业爬虫学习笔记day5
    cookie规范(RFC6265)翻译
    [POJ] 1511 Invitation Cards
  • 原文地址:https://www.cnblogs.com/matd/p/12855869.html
Copyright © 2011-2022 走看看