获取手机设备的mac地址

获取手机设备的mac地址

由于项目中需要获取手机mac地址作为标识此手机的唯一标识,于是就在网上搜如何获取mac地址的资料,目前看到最多的就是

public String getLocalMacAddress() {
                 WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
                WifiInfo info = wifi.getConnectionInfo();
                return info.getMacAddress();
             }

根据wifi信息来获取的,但是此方法是在wifi连接的时候才能获取,断开wifi就不能了,不符合要求,继续找,后来发现一个
public static String getLocalIpAddress()
	 {
		 try { 

             String ipv4; 

                 ArrayList  nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); 

             for (NetworkInterface ni: nilist)  

             { 

               ArrayList ialist = Collections.list(ni.getInetAddresses()); 

                for (InetAddress address: ialist)

       { 

                        if (!address.isLoopbackAddress() &&InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress()))  

                   {  

                       return ipv4; 

                   } 

               } 

           } 

          } catch (SocketException ex) { 
       } 

       return null; 
	 }
	 
	public static String getLocalMacAddressFromIp(Context context)
	 {
		 String mac_s= "";

	     try {

	             byte[] mac;

	             NetworkInterface ne =

	                      NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));

	             mac = ne.getHardwareAddress();
	             System.out.println("mac======="+mac);
	             mac_s = byte2hex(mac);

	             } catch (Exception e) {

	                          e.printStackTrace();
	             }

	         return mac_s;
	 }
	 
	 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);

	  }
根据ip地址来获取的,还是不行,因为ip地址是一定要在有网的情况下才有的,否则是0。


最后发现一个,此方法在断网的情况下能获取到,OK,解决

  public String getMacAddress(){  
	        String macSerial = null;
            String str = "";
            try {
                    Process pp = Runtime.getRuntime().exec(
                                    "cat /sys/class/net/wlan0/address ");
                    InputStreamReader ir = new InputStreamReader(pp.getInputStream());
                    LineNumberReader input = new LineNumberReader(ir);


                    for (; null != str;) {
                            str = input.readLine();
                            if (str != null) {
                                    macSerial = str.trim();// 去空格
                                    break;
                            }
                    }
            } catch (IOException ex) {
                    // 赋予默认值
                    ex.printStackTrace();
            }
            return macSerial;
	    }  

不好意思,上面的方法有时候也获取不到,所以如果要取设备的一个参数当做唯一标识,还是取DEVICE_ID,比较靠谱

/**
	 * 获取设备唯一标识DEVICE_ID
	 * @return
	 */
	    public String getDeviceID()
	    {
	    	TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); 
	    	String DEVICE_ID = tm.getDeviceId(); 
	    	return DEVICE_ID;
	    }


你可能感兴趣的:(android)