Get ethernet router MAC address in android

2k views Asked by At

I use this function to get device MAC address

public static String getMacAddress(){
        try {
            return loadFileAsString("/sys/class/net/eth0/address")
                    .toUpperCase().substring(0, 17);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

Now I need to also get mac address of router this ethernet is connected to. How can I do that?

1

There are 1 answers

1
Avi Levin On

You should use getBSSID() of WifiInfo class will return the MAC address of remote access point.

According to google docs it returns the basic service set identifier (BSSID) of the current access point in a form of mac address. The BSSID may be null if there is no network currently connected.

See here an example:

public String getMacId() {

    WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
    return wifiInfo.getBSSID();
}