本文整理汇总了Java中java.net.NetworkInterface.getHardwareAddress方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInterface.getHardwareAddress方法的具体用法?Java NetworkInterface.getHardwareAddress怎么用?Java NetworkInterface.getHardwareAddress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.NetworkInterface
的用法示例。
在下文中一共展示了NetworkInterface.getHardwareAddress方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createMachineIdentifier
import java.net.NetworkInterface; //导入方法依赖的package包/类
private static int createMachineIdentifier () {
// build a 2-byte machine piece based on NICs info
int machinePiece;
try {
StringBuilder stringBuilder = new StringBuilder();
Enumeration< NetworkInterface > networkInterfaces = NetworkInterface.getNetworkInterfaces();
while ( networkInterfaces.hasMoreElements() ) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
stringBuilder.append( networkInterface.toString() );
byte[] mac = networkInterface.getHardwareAddress();
if ( mac != null ) {
ByteBuffer byteBuffer = ByteBuffer.wrap( mac );
try {
stringBuilder.append( byteBuffer.getChar() );
stringBuilder.append( byteBuffer.getChar() );
stringBuilder.append( byteBuffer.getChar() );
} catch ( BufferUnderflowException shortHardwareAddressException ) { //NOPMD
// mac with less than 6 bytes. continue
}
}
}
machinePiece = stringBuilder.toString().hashCode();
} catch ( Throwable t ) {
// exception sometimes happens with IBM JVM, use random
machinePiece = new SecureRandom().nextInt();
}
return machinePiece;
}
示例2: getMACAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Returns MAC address of the given interface name.
*
* @param interfaceName eth0, wlan0 or NULL=use first interface
* @return mac address or empty string
*/
public static String getMACAddress(String interfaceName) {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (interfaceName != null) {
if (!intf.getName().equalsIgnoreCase(interfaceName)) continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac == null) return "";
StringBuilder buf = new StringBuilder();
for (byte aMac : mac) buf.append(String.format("%02X:", aMac));
if (buf.length() > 0) buf.deleteCharAt(buf.length() - 1);
return buf.toString();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return "";
}
示例3: getMacAddressByNetworkInterface
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 获取设备MAC地址
* <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p>
*
* @return MAC地址
*/
private static String getMacAddressByNetworkInterface() {
try {
List<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface ni : nis) {
if (!ni.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = ni.getHardwareAddress();
if (macBytes != null && macBytes.length > 0) {
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02x:", b));
}
return res1.deleteCharAt(res1.length() - 1).toString();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "02:00:00:00:00:00";
}
示例4: getMacAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* 実行中サーバの最初のネットワークインタフェースの Macアドレスを基にした文字列を返す。
* Macアドレスが取得できない場合、代替として現在日付の millisecondを 16進変換した文字列を返す。
* @return Macアドレスの文字列表現。ただしバイト間にデリミタは含まない。
*/
public static String getMacAddress() {
// 最初の NIC の MACアドレスを取得する。
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface nic = networkInterfaces.nextElement();
byte[] hardwareAddress = nic.getHardwareAddress();
StringBuilder buffer = new StringBuilder();
if (null != hardwareAddress) {
for (byte b : hardwareAddress) {
buffer.append(String.format("%02X", b));
}
return buffer.toString();
}
}
// nicが検出できないため、代替文字列を返す。
return Long.toHexString(System.currentTimeMillis());
} catch (SocketException e) {
// エラーで取得できないため、代替文字列を返す。
return Long.toHexString(System.currentTimeMillis());
}
}
示例5: testMachine
import java.net.NetworkInterface; //导入方法依赖的package包/类
@Override
public boolean testMachine() {
try {
final Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
final NetworkInterface networkInterface = networkInterfaces.nextElement();
final byte[] discoveredMacAddress = networkInterface.getHardwareAddress();
if (discoveredMacAddress != null) {
for (final byte[] macAddress : this.macAddresses) {
if (Arrays.equals(macAddress, discoveredMacAddress)) {
return true;
}
}
}
}
return false;
} catch (final SocketException e) {
throw new IllegalStateException(e);
}
}
示例6: getMACAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Returns MAC address of the given interface name.
*
* @param interfaceName eth0, wlan0 or NULL=use first interface
* @return mac address or empty string
*/
public static String getMACAddress(String interfaceName) {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface intf : interfaces) {
if (interfaceName != null) {
if (!intf.getName().equalsIgnoreCase(interfaceName)) continue;
}
byte[] mac = intf.getHardwareAddress();
if (mac == null) 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 (Exception ex) {
} // for now eat exceptions
return "";
/*try {
// this is so Linux hack
return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
} catch (IOException ex) {
return null;
}*/
}
示例7: writeToEntropyPool
import java.net.NetworkInterface; //导入方法依赖的package包/类
protected void writeToEntropyPool(DataOutputStream out) throws IOException {
out.writeLong(System.currentTimeMillis());
out.writeLong(System.nanoTime());
out.writeLong(Runtime.getRuntime().freeMemory());
List<NetworkInterface> ifaces =
Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface i : ifaces) {
List<InetAddress> addrs = Collections.list(i.getInetAddresses());
for (InetAddress a : addrs) out.write(a.getAddress());
byte[] hardware = i.getHardwareAddress();
if (hardware != null) out.write(hardware);
}
for (Entry<String, String> e : System.getenv().entrySet()) {
out.writeUTF(e.getKey());
out.writeUTF(e.getValue());
}
Properties properties = System.getProperties();
for (String key : properties.stringPropertyNames())
out.writeUTF(properties.getProperty(key));
}
示例8: getNetworkInterfaceNo
import java.net.NetworkInterface; //导入方法依赖的package包/类
public String getNetworkInterfaceNo()
{
String code = "";
try {
Enumeration<NetworkInterface> is = NetworkInterface.getNetworkInterfaces();
while (is.hasMoreElements()) {
NetworkInterface i = is.nextElement();
byte[] addr = i.getHardwareAddress();
if (addr != null) {
for (byte b : addr) {
code = code + (0xff & b);
}
}
}
} catch (SocketException ex) {
Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
}
return code;
}
示例9: concatMacAddresses
import java.net.NetworkInterface; //导入方法依赖的package包/类
private byte[] concatMacAddresses() throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (ni.getHardwareAddress() != null) {
bos.write(ni.getHardwareAddress());
}
}
return bos.toByteArray();
}
示例10: createMachineIdentifier
import java.net.NetworkInterface; //导入方法依赖的package包/类
/**
* Creates the machine identifier from the physical MAC address.
*
* @return long the machine identifier
*/
private static long createMachineIdentifier() {
byte[] mac = null;
try {
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
if (!ni.isLoopback()) {
mac = ni.getHardwareAddress();
}
// ?? mac[1] != (byte) 0xff it is from http://johannburkard.de/software/uuid/
if (mac != null && mac.length == 6 && mac[1] != (byte) 0xff) {
break;
} else {
continue;
}
}
} catch (Throwable t) {
throw new RuntimeException("Could not get MAC address", t);
}
if (mac != null && mac.length == 6 && mac[1] != (byte) 0xff) {
return bytes2long(mac);
} else {
if (null == mac) {
throw new RuntimeException("Could not get MAC address!");
} else {
throw new RuntimeException("MAC address is not correct:" + toHexString(mac));
}
}
}
示例11: getMacAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
@NonNull
private String getMacAddress(@NonNull String interfaceName) {
try {
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface networkInterface : interfaces) {
if (!networkInterface.getName().equalsIgnoreCase(interfaceName)) {
continue;
}
byte[] mac = networkInterface.getHardwareAddress();
if (mac == null) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
for (byte b : mac) {
stringBuilder.append(String.format("%02X:", b));
}
if (stringBuilder.length() > 0) {
stringBuilder.deleteCharAt(stringBuilder.length() - 1);
}
return stringBuilder.toString();
}
} catch (Exception e) {
e.printStackTrace();
return "";
}
return "";
}
示例12: getMacAddress
import java.net.NetworkInterface; //导入方法依赖的package包/类
public String getMacAddress() {
if(mWifiInfo == null) return "01:02:03:04:05:06";
String mac = mWifiInfo.getMacAddress();
if("02:00:00:00:00:00".equals(mac)){ //说明是android 6.0
//android 6.0 获取mac的代码,有异常就继续返回02:00:00:00:00:00
try {
List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
return "";
}
StringBuilder res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
return res1.toString();
}
} catch (Exception ex) {
}
}
return mac;
}
示例13: getHWSeq
import java.net.NetworkInterface; //导入方法依赖的package包/类
static private long getHWSeq()
{
if (hwseq != 0)
return hwseq;
for (int ii = 0; ii < 50; ii++)
{
try
{
NetworkInterface ni = NetworkInterface.getByIndex(ii);
if (ni != null) // real interface
{
byte[] hwaddr = ni.getHardwareAddress();
if (hwaddr == null) continue;
String dname = ni.getDisplayName();
if (dname.startsWith("Microsoft")) continue;
if (dname.startsWith("VMware")) continue;
if (dname.startsWith("VirtualBox")) continue;
if (dname.contains("Tunneling")) continue;
// HW will be somewhat unique, we use the above to skip things that generally are not
hwseq = (VARIANT1 & VARIANT_MASK)| (new Random().nextLong() & CLKSEQ_MASK) | (new BigInteger(1, hwaddr).longValue() & NODE_MASK);
break;
}
}
catch (SocketException se) {}
}
return hwseq;
}
示例14: createMacAddrString
import java.net.NetworkInterface; //导入方法依赖的package包/类
private static String createMacAddrString(NetworkInterface netIf)
throws SocketException {
byte[] macAddr = netIf.getHardwareAddress();
StringBuilder sb = new StringBuilder();
if (macAddr != null) {
for (int i = 0; i < macAddr.length; i++) {
sb.append(String.format("%02X%s", macAddr[i],
(i < macAddr.length - 1) ? "-" : ""));
}
}
return sb.toString();
}
示例15: getMacOsIdentifier
import java.net.NetworkInterface; //导入方法依赖的package包/类
private static String getMacOsIdentifier() throws SocketException, NoSuchAlgorithmException {
NetworkInterface networkInterface = NetworkInterface.getByName("en0");
byte[] hardwareAddress = networkInterface.getHardwareAddress();
return Utils.hexStringify(Utils.sha256Hash(hardwareAddress));
}