zoukankan      html  css  js  c++  java
  • android 获取ip地址和mac地址的多种方式

    部分来自网络:
     
            /**
         * 获取移动设备本地IP
         * @return
         */
        protected static InetAddress getLocalInetAddress() {
            InetAddress ip = null;
            try {
                //列举
                Enumeration<networkinterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
                while (en_netInterface.hasMoreElements()) {//是否还有元素
                    NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();//得到下一个元素
                    Enumeration<inetaddress> en_ip = ni.getInetAddresses();//得到一个ip地址的列举
                    while (en_ip.hasMoreElements()) {
                        ip = en_ip.nextElement();
                        if (!ip.isLoopbackAddress() && ip.getHostAddress().indexOf(":") == -1)
                            break;
                        else
                            ip = null;
                    }
     
                    if (ip != null) {
                        break;
                    }
                }
            } catch (SocketException e) {
     
                e.printStackTrace();
            }
            return ip;
        }
     
    </inetaddress></networkinterface>
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
        
        /**
     * 获取本地IP
     * @return
     */
    public static String getLocalIpAddress() { 
        try { 
            for (Enumeration<networkinterface> en = NetworkInterface 
                    .getNetworkInterfaces(); en.hasMoreElements();) { 
                NetworkInterface intf = en.nextElement(); 
                for (Enumeration<inetaddress> enumIpAddr = intf 
                        .getInetAddresses(); enumIpAddr.hasMoreElements();) { 
                    InetAddress inetAddress = enumIpAddr.nextElement(); 
                    if (!inetAddress.isLoopbackAddress()) { 
                        return inetAddress.getHostAddress().toString(); 
                    } 
                } 
            } 
        } catch (SocketException ex) { 
            ex.printStackTrace();
        } 
        return null; 
    }</inetaddress></networkinterface>
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
        
    /**
         * 根据IP地址获取MAC地址
         * @return
         */
        @SuppressLint({ "NewApi", "DefaultLocale" })
        public static String getMacAddress(){
            String strMacAddr = null;
            try {
                //获得IpD地址
                InetAddress ip = getLocalInetAddress();
                byte[] b = NetworkInterface.getByInetAddress(ip).getHardwareAddress();
                StringBuffer buffer = new StringBuffer();
                for (int i = 0; i < b.length; i++) {
                    if (i != 0) { buffer.append(':');
                    }
                    String str = Integer.toHexString(b[i] & 0xFF);
                    buffer.append(str.length() == 1 ? 0 + str : str);
                }
                strMacAddr = buffer.toString().toUpperCase();
            } catch (Exception e) {
     
            }
     
            return strMacAddr;
        }
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
        
       /**
     * 根据IP获取本地Mac
     * @param context
     * @return
     */
    public static String getLocalMacAddressFromIp(Context context) {
        String mac_s= "";
        try {
            byte[] mac;
            NetworkInterface ne=NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
            mac = ne.getHardwareAddress();
            mac_s = byte2hex(mac);
        } catch (Exception e) {
            e.printStackTrace();
        }
     
        return mac_s;
    }
     
    /**
     * 二进制转十六进制
     * @param b
     * @return
     */
    public static  String byte2hex(byte[] b) {
        StringBuffer hs = new StringBuffer(b.length);
        String stmp = "";
        int len = b.length;
        for (int n = 0; n < len; n++) {
            stmp = Integer.toHexString(b[n] & 0xFF);
            if (stmp.length() == 1)
                hs = hs.append("0").append(stmp);
            else {
                hs = hs.append(stmp);
            }
        }
        return String.valueOf(hs);
    }
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
        
        /**
     * 根据busybox获取本地Mac
     * @return
     */
    public static String getLocalMacAddressFromBusybox(){  
        String result = "";    
        String Mac = "";
        result = callCmd("busybox ifconfig","HWaddr");
        //如果返回的result == null,则说明网络不可取
        if(result==null){
            return "网络异常";
        }
        //对该行数据进行解析
        //例如:eth0      Link encap:Ethernet  HWaddr 00:16:E8:3E:DF:67
        if(result.length()>0 && result.contains("HWaddr")==true){
            Mac = result.substring(result.indexOf("HWaddr")+6, result.length()-1);
            result = Mac;
        }
        return result;
    }  
     
    private static String callCmd(String cmd,String filter) {  
        String result = "";  
        String line = "";  
        try {
            Process proc = Runtime.getRuntime().exec(cmd);
            InputStreamReader is = new InputStreamReader(proc.getInputStream());  
            BufferedReader br = new BufferedReader (is);  
             
            while ((line = br.readLine ()) != null && line.contains(filter)== false) {  
                result += line;
            }
     
            result = line;
        }  
        catch(Exception e) {  
            e.printStackTrace();  
        }  
        return result;  
    }
    
    
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
        
        /**
     * 根据wifi信息获取本地mac
     * @param context
     * @return
     */
    public static String getLocalMacAddressFromWifiInfo(Context context){
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); 
        WifiInfo winfo = wifi.getConnectionInfo(); 
                String mac =  winfo.getMacAddress();
        return mac;
    }
  • 相关阅读:
    浅谈Java中的对象和对象引用
    学习Python要知道哪些重要的库和工具
    Python新手最容易犯的十大错误
    java截取字符串中的数字
    Android 多语言
    R语言学习笔记(二): 类与泛型函数
    R语言学习笔记(一):mode, class, typeof的区别
    代理实现方式
    如何将链表反转
    同步锁之lock
  • 原文地址:https://www.cnblogs.com/yico/p/3494201.html
Copyright © 2011-2022 走看看