android 获取设备的IP和Mac地址

获取设备ip:

	public String GetHostIp() {
		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()
							&& InetAddressUtils.isIPv4Address(inetAddress
									.getHostAddress())) {
						if (!inetAddress.getHostAddress().toString()
								.equals("null")
								&& inetAddress.getHostAddress() != null) {
							return inetAddress.getHostAddress().toString().trim();
						}
					}
				}
			}
		} catch (SocketException ex) {
			Log.e("WifiPreference IpAddress", ex.toString());
		}
		return "";
	}

获取设备Mac地址

public String getLocalMacAddress() {
		try{
			String path="sys/class/net/eth0/address";
			FileInputStream fis_name = new FileInputStream(path);
			byte[] buffer_name = new byte[1024*8];
	        int byteCount_name = fis_name.read(buffer_name);
	        if(byteCount_name>0)
	        {
	            mac = new String(buffer_name, 0, byteCount_name, "utf-8");
	        }
	        
	        if(mac.length()==0||mac==null){
	        	path="sys/class/net/eth0/wlan0";
	        	FileInputStream fis = new FileInputStream(path);
				byte[] buffer = new byte[1024*8];
		        int byteCount = fis.read(buffer);
		        if(byteCount>0)
		        {
		            mac = new String(buffer, 0, byteCount, "utf-8");
		        }
	        }
	        
	        if(mac.length()==0||mac==null){
	        	return "";
	        }
		}catch(Exception io){
			
		}
		return mac.trim();
	} 
}


你可能感兴趣的:(android 获取设备的IP和Mac地址)