List of usage examples for java.net NetworkInterface getByInetAddress
public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException
From source file:net.chunkyhosting.Roe.ChunkyTransactions.api.License.java
public boolean checkMac(String mac) { StringBuilder sb = new StringBuilder(); try {/* ww w.ja va 2s. c o m*/ NetworkInterface network = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()); byte[] macAddr = network.getHardwareAddress(); for (int i = 0; i < macAddr.length; i++) { sb.append(String.format("%02X%s", macAddr[i], (i < macAddr.length - 1) ? "-" : "")); } ChunkyTransactions.getInstance().getMessage().debug("Your Mac Address: " + sb.toString()); this.setMac(sb.toString()); if (sb.toString().equalsIgnoreCase(mac)) { return true; } } catch (SocketException e) { ChunkyTransactions.getInstance().getMessage().log(Level.SEVERE, "Unable to check your Mac Address"); ChunkyTransactions.getInstance().getMessage().spitError(e); return false; } catch (UnknownHostException e) { ChunkyTransactions.getInstance().getMessage().log(Level.SEVERE, "Unable to check your Mac Address"); ChunkyTransactions.getInstance().getMessage().spitError(e); return false; } return false; }
From source file:com.mozilla.SUTAgentAndroid.SUTAgentAndroid.java
/** Called when the activity is first created. */ @Override//from ww w. jav a 2 s. co m public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); fixScreenOrientation(); DoCommand dc = new DoCommand(getApplication()); Log.i("SUTAgentAndroid", dc.prgVersion); dc.FixDataLocalPermissions(); // Get configuration settings from "ini" file File dir = getFilesDir(); File iniFile = new File(dir, "SUTAgent.ini"); String sIniFile = iniFile.getAbsolutePath(); String lc = dc.GetIniData("General", "LogCommands", sIniFile); if (lc != "" && Integer.parseInt(lc) == 1) { SUTAgentAndroid.LogCommands = true; } SUTAgentAndroid.RegSvrIPAddr = dc.GetIniData("Registration Server", "IPAddr", sIniFile); SUTAgentAndroid.RegSvrIPPort = dc.GetIniData("Registration Server", "PORT", sIniFile); SUTAgentAndroid.HardwareID = dc.GetIniData("Registration Server", "HARDWARE", sIniFile); SUTAgentAndroid.Pool = dc.GetIniData("Registration Server", "POOL", sIniFile); SUTAgentAndroid.sTestRoot = dc.GetIniData("Device", "TestRoot", sIniFile); SUTAgentAndroid.Abi = android.os.Build.CPU_ABI; log(dc, "onCreate"); dc.SetTestRoot(SUTAgentAndroid.sTestRoot); Log.i("SUTAgentAndroid", "Test Root: " + SUTAgentAndroid.sTestRoot); tv = (TextView) this.findViewById(R.id.Textview01); if (getLocalIpAddress() == null) setUpNetwork(sIniFile); String macAddress = "Unknown"; if (android.os.Build.VERSION.SDK_INT > 8) { try { NetworkInterface iface = NetworkInterface .getByInetAddress(InetAddress.getAllByName(getLocalIpAddress())[0]); if (iface != null) { byte[] mac = iface.getHardwareAddress(); if (mac != null) { StringBuilder sb = new StringBuilder(); Formatter f = new Formatter(sb); for (int i = 0; i < mac.length; i++) { f.format("%02x%s", mac[i], (i < mac.length - 1) ? ":" : ""); } macAddress = sUniqueID = sb.toString(); } } } catch (UnknownHostException ex) { } catch (SocketException ex) { } } else { // Fall back to getting info from wifiman on older versions of Android, // which don't support the NetworkInterface interface WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE); if (wifiMan != null) { WifiInfo wifi = wifiMan.getConnectionInfo(); if (wifi != null) macAddress = wifi.getMacAddress(); if (macAddress != null) sUniqueID = macAddress; } } if (sUniqueID == null) { BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter(); if ((ba != null) && (ba.isEnabled() != true)) { ba.enable(); while (ba.getState() != BluetoothAdapter.STATE_ON) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } sUniqueID = ba.getAddress(); ba.disable(); while (ba.getState() != BluetoothAdapter.STATE_OFF) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } else { if (ba != null) { sUniqueID = ba.getAddress(); sUniqueID.toLowerCase(); } } } if (sUniqueID == null) { TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE); if (mTelephonyMgr != null) { sUniqueID = mTelephonyMgr.getDeviceId(); if (sUniqueID == null) { sUniqueID = "0011223344556677"; } } } String hwid = getHWID(this); sLocalIPAddr = getLocalIpAddress(); Toast.makeText(getApplication().getApplicationContext(), "SUTAgent [" + sLocalIPAddr + "] ...", Toast.LENGTH_LONG).show(); String sConfig = dc.prgVersion + lineSep; sConfig += "Test Root: " + sTestRoot + lineSep; sConfig += "Unique ID: " + sUniqueID + lineSep; sConfig += "HWID: " + hwid + lineSep; sConfig += "ABI: " + Abi + lineSep; sConfig += "OS Info" + lineSep; sConfig += "\t" + dc.GetOSInfo() + lineSep; sConfig += "Screen Info" + lineSep; int[] xy = dc.GetScreenXY(); sConfig += "\t Width: " + xy[0] + lineSep; sConfig += "\t Height: " + xy[1] + lineSep; sConfig += "Memory Info" + lineSep; sConfig += "\t" + dc.GetMemoryInfo() + lineSep; sConfig += "Network Info" + lineSep; sConfig += "\tMac Address: " + macAddress + lineSep; sConfig += "\tIP Address: " + sLocalIPAddr + lineSep; displayStatus(sConfig); sRegString = "NAME=" + sUniqueID; sRegString += "&IPADDR=" + sLocalIPAddr; sRegString += "&CMDPORT=" + 20701; sRegString += "&DATAPORT=" + 20700; sRegString += "&OS=Android-" + dc.GetOSInfo(); sRegString += "&SCRNWIDTH=" + xy[0]; sRegString += "&SCRNHEIGHT=" + xy[1]; sRegString += "&BPP=8"; sRegString += "&MEMORY=" + dc.GetMemoryConfig(); sRegString += "&HARDWARE=" + HardwareID; sRegString += "&POOL=" + Pool; sRegString += "&ABI=" + Abi; String sTemp = Uri.encode(sRegString, "=&"); sRegString = "register " + sTemp; pruneCommandLog(dc.GetSystemTime(), dc.GetTestRoot()); if (!bNetworkingStarted) { Thread thread = new Thread(null, doStartService, "StartServiceBkgnd"); thread.start(); bNetworkingStarted = true; Thread thread2 = new Thread(null, doRegisterDevice, "RegisterDeviceBkgnd"); thread2.start(); } monitorBatteryState(); // If we are returning from an update let'em know we're back Thread thread3 = new Thread(null, doUpdateCallback, "UpdateCallbackBkgnd"); thread3.start(); final Button goButton = (Button) findViewById(R.id.Button01); goButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { finish(); } }); }
From source file:org.ow2.proactive.scheduler.util.SchedulerStarter.java
public static boolean isThisMyIpAddress(InetAddress addr) { // Check if the address is a valid special local or loop back if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) return true; // Check if the address is defined on any interface try {/*from ww w. j a v a 2 s. c om*/ return NetworkInterface.getByInetAddress(addr) != null; } catch (SocketException e) { return false; } }
From source file:in.bbat.license.LicenseManager.java
public static String getMacId() { try {//from w w w . j a v a 2 s .co m InetAddress localInetAddress = InetAddress.getLocalHost(); NetworkInterface localNetworkInterface = NetworkInterface.getByInetAddress(localInetAddress); byte[] arrayOfByte = localNetworkInterface.getHardwareAddress(); StringBuilder localStringBuilder = new StringBuilder(); for (int i = 0; i < arrayOfByte.length; i++) localStringBuilder.append(String.format("%02X%s", new Object[] { Byte.valueOf(arrayOfByte[i]), i < arrayOfByte.length - 1 ? "-" : "" })); return localStringBuilder.toString(); } catch (Exception localException) { } return ""; }
From source file:org.programmatori.domotica.own.plugin.system.System.java
private Value getMac() { Value v = null;/*from www . ja v a2 s. c o m*/ try { InetAddress thisIp = InetAddress.getLocalHost(); boolean first = true; NetworkInterface networkInterface = NetworkInterface.getByInetAddress(thisIp); byte[] idr = networkInterface.getHardwareAddress(); if (idr != null && idr.length > 0) { String val = "0"; for (int z = 0; z < idr.length; z++) { if (idr[z] < 0) { val = Integer.toString(256 + idr[z]); } else { val = Integer.toString(idr[z]); } if (first) { v = new Value(val); first = false; } else { v.addValue(val); } } } } catch (Exception e) { e.printStackTrace(); } return v; }
From source file:org.apache.hadoop.net.TestDNS.java
private String getLoopbackInterface() throws SocketException { return NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress()).getName(); }
From source file:org.programmatori.domotica.own.plugin.system.System.java
private Value getNetMask() { Value v = null;/*from w w w. j a va 2 s .c om*/ try { InetAddress thisIp = InetAddress.getLocalHost(); String ip = ""; NetworkInterface networkInterface = NetworkInterface.getByInetAddress(thisIp); short mask = networkInterface.getInterfaceAddresses().get(0).getNetworkPrefixLength(); switch (mask) { // IPv4 case 8: ip = "255.0.0.0"; break; case 16: ip = "255.255.0.0"; break; case 24: ip = "255.255.255.0"; break; // IPv6 // case 128: // ip = "::1/128"; // break; // // case 10: // ip = "fe80::203:baff:fe27:1243/10"; // break; default: ip = "255.255.255.0"; break; } for (int i = 0; i < 3; i++) { if (i == 0) { v = new Value(ip.substring(0, ip.indexOf('.'))); // IP Part } else { v.addValue(ip.substring(0, ip.indexOf('.'))); // IP Part } ip = ip.substring(ip.indexOf('.') + 1); } v.addValue(ip); // IP End Part } catch (Exception e) { e.printStackTrace(); } return v; }
From source file:org.basdroid.common.NetworkUtils.java
public static String getMacAddressFromNetworkInterface(final Context context) { WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); // Convert little-endian to big-endianif needed if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) { ipAddress = Integer.reverseBytes(ipAddress); }/*w w w . jav a 2 s. c o m*/ byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray(); String result; try { InetAddress addr = InetAddress.getByAddress(bytes); NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr); Log.d(TAG, "Wifi netInterface.getName() = " + netInterface.getName()); byte[] mac = netInterface.getHardwareAddress(); if (mac == null || mac.length == 0) return ""; StringBuilder buf = new StringBuilder(); for (int idx = 0; idx < mac.length; idx++) { buf.append(String.format("%02X:", mac[idx])); } if (buf.length() > 0) buf.deleteCharAt(buf.length() - 1); return buf.toString(); } catch (UnknownHostException ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Unknown host.", ex); result = null; } catch (SocketException ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Socket exception.", ex); result = null; } catch (Exception ex) { Log.e(TAG, "getMacAddressFromNetworkInterface() Exception.", ex); result = null; } return result; }
From source file:backend.SoftwareSecurity.java
private String[] getMacAndIPAddresses() throws IOException { String macAddress;/*from w ww .jav a 2 s . c o m*/ String ipAddress; //get ip ipAddress = ("External:'" + new BufferedReader(new InputStreamReader(new URL("http://checkip.amazonaws.com").openStream())) .readLine() + "',Host:" + InetAddress.getLocalHost().getHostAddress() + "'"); //get mac byte[] mac = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).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) ? "-" : "")); macAddress = sb.toString(); //return the values as a string array return new String[] { ipAddress, macAddress }; }
From source file:org.saadahmed.snowcrystal.SnowCrystal.java
public static byte[] getMacAddress() { byte[] address = new byte[NODE_LENGTH]; try {// ww w. jav a 2 s. co m InetAddress ip = InetAddress.getLocalHost(); byte[] macAddress = NetworkInterface.getByInetAddress(ip).getHardwareAddress(); // NODE_LENGTH = 6 if (macAddress.length < SnowCrystal.NODE_LENGTH) { System.arraycopy(macAddress, 0, address, 0, macAddress.length); return address; } // NODE_LENGTH = 6 else if (macAddress.length == SnowCrystal.NODE_LENGTH) { return macAddress; } // NODE_LENGTH = 6 else if (macAddress.length > SnowCrystal.NODE_LENGTH && macAddress.length == 8) { System.arraycopy(macAddress, 0, address, 0, SnowCrystal.NODE_LENGTH / 2); System.arraycopy(macAddress, (SnowCrystal.NODE_LENGTH / 2) + 2, address, (SnowCrystal.NODE_LENGTH / 2), SnowCrystal.NODE_LENGTH / 2); return address; } // NODE_LENGTH = 6 else if (macAddress.length > SnowCrystal.NODE_LENGTH && macAddress.length != 8) { throw new UnknownHostException("Unknown host address type"); } } catch (Exception e) { e.printStackTrace(); new Random().nextBytes(address); } return address; }