0
votes

I have function for read MAC address in java:

    for(Enumeration<NetworkInterface> enm = NetworkInterface.getNetworkInterfaces(); enm.hasMoreElements();){
        NetworkInterface network = (NetworkInterface) enm.nextElement();
          if(null != network.getHardwareAddress()){
              System.out.print(network.getDisplayName());
              byte[] mac = network.getHardwareAddress();
              StringBuilder sb = new StringBuilder(18);
              for (byte b : mac) {
                  if (sb.length() > 0)
                      sb.append(':');
                  sb.append(String.format("%02x", b & 0xff));
              }                  

              System.out.print(sb.toString());
              return null; 
          }
       }

This result: wlan0: 00:25:d3:9c:ad:7a But when I do: iwconfig, I reveive: 00:0D:F3:0D:DD:DC for wlan0

Question, why ?

EDIT: I am sorry for question, When I run ifconfig (not iwconfig) it responds:

wlan0 Link encap:Ethernet HWaddr 00:25:d3:9c:ad:7a
inet addr:192.168.50.100 Bcast:192.168.50.255 Mask:255.255.255.0 inet6 addr: fe80::225:d3ff:fe9c:ad7a/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:2417546 errors:0 dropped:0 overruns:0 frame:0 TX packets:1608679 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:3099222889 (3.0 GB) TX bytes:183978636 (183.9 MB)

So it's okay, thank you anyway.

1
One is for an AzureWave based product, one is for an Asmax based product, so seemingly two different wireless cards. Are you sure that you're running iwconfig on the same machine as you're executing the Java program? - Joachim Isaksson
Yes, I am sure it is one machine :) - mitch

1 Answers

0
votes

Try running this code:

    for (Enumeration<NetworkInterface> enm = NetworkInterface
            .getNetworkInterfaces(); enm.hasMoreElements();) {
        NetworkInterface network = (NetworkInterface) enm.nextElement();
        if (null != network.getHardwareAddress()) {
            System.out.print(network.getDisplayName() + " is ");
            byte[] mac = network.getHardwareAddress();
            StringBuilder sb = new StringBuilder(18);
            for (byte b : mac) {
                if (sb.length() > 0)
                    sb.append(':');
                sb.append(String.format("%02x", b & 0xff));
            }

            System.out.println(sb.toString());
        }
    }

This will print out the names/mac addresses of all the network interfaces, your old code just did the first one. See if any of these mac addresses match the one being given by iwconfig