Visualizzazione dei risultati da 1 a 2 su 2
  1. #1
    Utente di HTML.it
    Registrato dal
    Dec 2009
    Messaggi
    1,123

    [Java] MAC Address: problema con getHardwareAddress()

    Ciao a tutti,

    Devo ottenere il MAC Address, ma ho un problemuccio.

    Ho già letto la doc in merito a NetworkAddress, ed in particolare
    Returns the hardware address (usually MAC) of the interface if it has one and if it can be accessed given the current privileges.

    Returns:
    a byte array containing the address or null if the address doesn't exist or is not accessible.
    ma non riesco a venirne a capo...
    A me il programma ritorna sempre null insomma.
    Ecco il codice:
    codice:
    import java.util.*;
    import java.net.*;
    import java.io.*;
    
    class NetworkUtility {
      private NetworkUtility() {}
      
      public static String getMacAddress() {
        try {
          StringBuffer sb = new StringBuffer();
          Enumeration<NetworkInterface> enumerazione = NetworkInterface.getNetworkInterfaces();
    	  while(enumerazione.hasMoreElements()) {
    	    NetworkInterface el = enumerazione.nextElement();
    	    System.out.println ("Interface: " + el.getDisplayName().trim());
    	    byte[] addr = el.getHardwareAddress();
    		System.out.println("EL "+el);
    		if(addr == null) return null;
    	    for(int i=0; i<addr.length; i++) {
    	      sb.append(String.format(i == 0 ? "%02X" : "-%02X", addr[i]));
    	    }
    	  }
    	  return sb.toString();
        } catch(Exception e) {
    	  return e.toString();
    	}
      }
    }
    
    class TestMac {
      public static void main(String[] args) {
        System.out.println(NetworkUtility.getMacAddress());
      }
    }
    Grazie a tutti! ^^

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    io ho trovato e testato (positivamente) questa:
    codice:
    import java.net.InetAddress;
    import java.net.NetworkInterface;
    import java.net.SocketException;
    import java.net.UnknownHostException;
    
    public class MacAddress {
    
        public static void main(String[] args) {
            try {
                InetAddress address = InetAddress.getLocalHost();
                //InetAddress address = InetAddress.getByName("192.168.2.2");
    
                /*
                 * Get NetworkInterface for the current host and then read the
                 * hardware address.
                 */
                NetworkInterface ni = NetworkInterface.getByInetAddress(address);
                if (ni != null) {
                    byte[] mac = ni.getHardwareAddress();
                    if (mac != null) {
                        /*
                         * Extract each array of mac address and convert it to hexa with the
                         * following format 08-00-27-DC-4A-9E.
                         */
                        for (int i = 0; i < mac.length; i++) {
                            System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                        }
                    } else {
                        System.out.println("Address doesn't exist or is not accessible.");
                    }
                } else {
                    System.out.println("Network Interface for the specified address is not found.");
                }
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    }
    e questo è il link al sorgente originale:
    http://www.kodejava.org/examples/250.html
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.