Android机顶盒取网卡的Mac地址

来源:互联网 发布:编程员工资 编辑:程序博客网 时间:2024/04/28 01:52

android机顶盒上一般有两个网卡,一个有线一个无线,那么在获取mac地址来唯一标识一台终端的时候取那个呢?经过讨论,取有线的Mac地址。但是当前如果我只用的是wifi呢?怎么取有线网卡的mac地址,经过观察settings里面的源码,发现了获取有线网卡mac地址的方法,即使当前机顶盒连接时wifi,如下:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. public String getEth0HW(){  
  2.     EthernetManager ethManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE);  
  3.     String str = ethManager.getEthernetHwaddr(ethManager.getEthernetIfaceName());  
  4.     if(null == str){  
  5.         str = "null";  
  6.     }  
  7.     return str ;  
  8. }  


/**
* 获取wifi模块的mac地址,即使wifi是关闭的,需要添加权限 ACCESS_WIFI_STATE
* @param context
* @return
*/
public static String getWifiMac(Context context){
       WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
       WifiInfo info = wifi.getConnectionInfo();
       String mac = info.getMacAddress();
       Log.e("","wifi mac : " + mac);
       return mac ;
    }


下面的方法是取当前连接网络的mac地址:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 获取当前连接网络的网卡的mac地址  
  2. private static String parseByte(byte b) {  
  3.         String s = "00" + Integer.toHexString(b)+":";  
  4.         return s.substring(s.length() - 3);  
  5. }  
  6.   
  7.     /** 
  8.      * 获取当前系统连接网络的网卡的mac地址 
  9.      * @return 
  10.      */  
  11.     @SuppressLint("NewApi")  
  12.     public static final String getMac() {  
  13.         byte[] mac = null;  
  14.         StringBuffer sb = new StringBuffer();  
  15.         try {  
  16.             Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();  
  17.             while (netInterfaces.hasMoreElements()) {  
  18.                 NetworkInterface ni = netInterfaces.nextElement();  
  19.                 Enumeration<InetAddress> address = ni.getInetAddresses();  
  20.                   
  21.                 while (address.hasMoreElements()) {  
  22.                     InetAddress ip = address.nextElement();  
  23.                     if (ip.isAnyLocalAddress() || !(ip instanceof Inet4Address) || ip.isLoopbackAddress())  
  24.                         continue;  
  25.                     if (ip.isSiteLocalAddress())  
  26.                         mac = ni.getHardwareAddress();  
  27.                     else if (!ip.isLinkLocalAddress()) {  
  28.                         mac = ni.getHardwareAddress();  
  29.                         break;  
  30.                     }  
  31.                 }  
  32.             }  
  33.         } catch (SocketException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.           
  37.         if(mac != null){  
  38.             for(int i=0 ;i<mac.length ;i++){  
  39.                 sb.append(parseByte(mac[i]));  
  40.             }  
  41.             return sb.substring(0, sb.length()-1);  
  42.         }else{  
  43.             return UpdateService.mDefaultMacAddress;  
  44.         }  
  45.     }  
  46. }  
转账自:http://blog.csdn.net/android_dong/article/details/17447841

0 0
原创粉丝点击