TavernerPOS - Source Code

Clone: 

git clone http://www.librarysmith.co.uk/tavernerPOS

 

package com.floreantpos.getnetinfo;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

/* CREDIT: Taken from http://www.mkyong.com/java/how-to-get-mac-address-in-java/ */

public class NetInfo {

	 public static String getIPAddr(){

			InetAddress ip;
			try {

				ip = InetAddress.getLocalHost();
				return(ip.getHostAddress());

			} catch (UnknownHostException e) {

				//e.printStackTrace();

			}
		return(null);
	 }

	 public static String getMACAddr() {

			InetAddress ip;
			try {

				ip = InetAddress.getLocalHost();

				NetworkInterface network = NetworkInterface.getByInetAddress(ip);

				byte[] mac = network.getHardwareAddress();

				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < mac.length; i++) {
					//sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
					sb.append(String.format("%03d%s", mac[i], (i < mac.length - 1) ? "-" : ""));
				}
				return(sb.toString());

			} catch (UnknownHostException e) {

				//e.printStackTrace();

			} catch (SocketException e){

				//e.printStackTrace();

			}

			return(null);

		   }

	 public static int getTerminalIDFromMACAddr() {

		 String mac=getMACAddr();
	 	 String terminalID = null;
		  if ( mac != null) {
		 	 terminalID=mac.replace("-","");
			 terminalID=terminalID.substring(terminalID.length() - 10);
		 	} else return(-1);
			return(Integer.parseInt(terminalID));
		  }


}

Add comment
These comments are moderated so so won't be published until reviewed.