java – Fetch unique identifier for a WiFi router-ThrowExceptions

Exception or error:

Is there any way I can get any unique identifier for a particular wifi router?

I’m trying to write an Android app that needs to know which router it is connected to. I know that android provides a way to get the BSSID of the currently connected network, but to my surprise, this is not unique.
I found out that on dual band routers, two different devices could be getting two different BSSID, even if they are on the same SSID.
Is there any other parameter I can fetch that can uniquely identify the wifi I am connected to? I would love to try some third party library, if that would allow me, since I am quite certain Android does not come packaged with a better method than giving me the BSSID.

Edit: I’m trying to find out who all have set their home wifi (via the app) as the same Wi-Fi as me. Each user sets their ‘home Wi-Fi’ which gets saved on the server ( the mac address is what I’m saving). Then each user can query who all are on their Wi-Fi, and if they are currently connected on that Wi-Fi or not. The query of ‘who is on my wifi’ is done by searching for the same MAC address as the one I’m connected to. This fails if my home has a dual band, since they could be connected to the second frequency (and thus second MAC).

How to solve:

You are correct in assuming that getBSSID() will return two different addresses for the two different bands, as they are essentially two different access points, one 2.4GHz and one 5 GHz, simply wrapped up in the same package. I wrote a quick app that gets and displays all of the available fields that may be obtained using the WifiInfo class. When I connected to the 2.4 GHz band of my access point the details were as follows:

enter image description here

Note that you can tell you are connected to the 2.4 GHz band (“Frequency: 2452”). Repeating the process with the 5 GHz band shows the following:

enter image description here

As you can see, the MAC addresses for the two different bands differ by only one number; I do not know if this is the case for all routers. If this is indeed the case, then you can conclusively determine which access point you are connected to by analyzing a combination of the MAC address (BSSID) and the frequency.

###

Sadly, there is no guaranteed method of uniquely identifying a specific access point. You can come close by using the SSID, MAC, etc. but it wouldn’t necessarily work every time and it would still be possible for someone to spoof this if they desired.

If you wish to consider different WiFi interfaces(1) (as opposed to the physical device supporting it) then the MAC address is, by definition, what you need (barring broken devices that wrongly re-use a MAC).

(1) I use the word “interfaces” because it’s possible to support multiple networks (i.e. SSIDs) on the same interface. An interface will effectively be a single channel at a single frequency.

###

Just use MAC address of the AP. (MAC Address is unique)

Here is how:
Can I find the MAC address of my Access point in Android?

###

For unique identifier, you can use
BSSI+Frequency

Example:-
AA:AA:AA:AA:AA:AA+2432 and AA:AA:AA:AA:AA:AA+5230 are unique in dual band case

###

You can find the Mac address of your router and use that as your basis for uniquely identifying it.

You can do so by checking your Android devices ARP table and compare the router IP that you are connected to. An example can be shown here:

http://www.flattermann.net/2011/02/android-howto-find-the-hardware-mac-address-of-a-remote-host/

Relevant code extracted:

/**
 * Try to extract a hardware MAC address from a given IP address using the
 * ARP cache (/proc/net/arp).<br>
 * <br>
 * We assume that the file has this structure:<br>
 * <br>
 * IP address       HW type     Flags       HW address            Mask     Device
 * 192.168.18.11    0x1         0x2         00:04:20:06:55:1a     *        eth0
 * 192.168.18.36    0x1         0x2         00:22:43:ab:2a:5b     *        eth0
 *
 * @param ip
 * @return the MAC from the ARP cache
 */
public static String getMacFromArpCache(String ip) {
    if (ip == null)
        return null;
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] splitted = line.split(" +");
            if (splitted != null && splitted.length >= 4 && ip.equals(splitted[0])) {
                // Basic sanity check
                String mac = splitted[3];
                if (mac.matches("..:..:..:..:..:..")) {
                    return mac;
                } else {
                    return null;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

Leave a Reply

Your email address will not be published. Required fields are marked *