20

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).

Urban
  • 2,201
  • 24
  • 52
  • Why don't you simply use the name of the access point ? – Wildcopper Apr 17 '15 at 07:55
  • Two different houses can have the same access point name (SSID). So thats not unique. For example, a lot of people usually leave the SSID to be the default router name like 'linksys-123'. – Urban Apr 17 '15 at 07:57
  • Ah ok my bad, I thought it would be in a "controlled" environment. But I think using the "MAC" address of the access point would work. – Wildcopper Apr 17 '15 at 08:46
  • @Wildcopper that's what I thought. However, on a dual band router, you can have two MAC addresses, one for each band. Well, at least that's what I could perceive when the `getBSSID` method returned two different addresses on two different phones connected to the same WiFi. – Urban Apr 17 '15 at 08:49
  • Perhaps if you explained _why_ you want to do this we could suggest alternatives to accomplish the higher goal. – Brian White Apr 25 '15 at 15:06
  • @BrianWhite updated the question with what I'm trying to achieve. – Urban Apr 25 '15 at 16:17
  • Okay, that's "what" (original question is "how") but still hazy on "why". What's the purpose of knowing if you're on the same AP? What will you accomplish with this information? – Brian White Apr 25 '15 at 17:58
  • Well my app basically tells you which one of your roommates are home. I'm using wifi to determine if one is home or not. The mac address of the router is used to determine who all are my roommates. Basically who all live in this house. – Urban Apr 25 '15 at 18:52
  • @Urban, In that case, how about querying your public IP address and comparing that? It's probably dynamic but won't change very often. You'll match everybody using the same NAT firewall which is somewhat broader than the Access Point but might be okay for your purposes. Or you could also train the system to recognize multiple MAC addresses as being the same and match that way. Or there is always the "coarse" location service. – Brian White Apr 25 '15 at 20:10
  • @BrianWhite I'm not very sure about the method you described first, with the public IP address. How would that work again? Also, as you mention, those will change (agreed, not very often, but I think even then it may break the app). About training to recognize multiple MACs, also I'm not sure how wold that work since a single device won't even be aware of any other MACs apart from the one it is connected to. As for the third solution, I really wanted to stay clear of location services, but if that's the only solution, then I might have to reconsider my whole app. – Urban Apr 25 '15 at 23:35
  • For the first: Each device would use an external website to query what it's public IP address is and report it. All other devices using your app that report the same public IP are connected to the same NAT router. For the second: It would depend on the devices reporting information (SSID, GPS coords, etc.) that allow such a mapping to be built.; yes, somewhat difficult. – Brian White Apr 26 '15 at 03:08

5 Answers5

7

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.

Community
  • 1
  • 1
Willis
  • 5,308
  • 3
  • 32
  • 61
  • I did pull in all these above details for the dual band I was connected to, and had noticed that on a few of them, the BSSID differed at the 3rd, or even the 2nd par of characters (differ at the o in xx:oo:xx:xx:xx:xx). This means that there isnt even a consistency in how they may differ for different bands. – Urban Apr 21 '15 at 19:00
  • 3
    If you consider that the 2.4 and 5 GHz bands are essentially individual routers with unique MAC addresses then I do not know if there is a way determining if any given 2.4 GHz and 5 GHz band belong to the same physical access point – Willis Apr 21 '15 at 20:18
  • hmm, I hope theres some way to do it. – Urban Apr 21 '15 at 23:08
  • MAC addresses are assigned to companies in blocks of 16 million. How these are assigned to actual interfaces is up to the manufacturer and though it's common to assign them sequentially, that is not always the case. Sometimes they even use the same MAC on different interfaces of a device even though you're not supposed to do that. – Brian White Apr 25 '15 at 15:03
  • I'm giving this answer the bounty amount, due to the effort put in for their research. However, since this didnt exactly 'solve' my problem, I'm not marking it as the answer. Thanks @Willis – Urban Apr 26 '15 at 16:17
2

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.

Brian White
  • 8,332
  • 2
  • 43
  • 67
1

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?

Community
  • 1
  • 1
yaser eftekhari
  • 235
  • 1
  • 6
  • 1
    That's what I've already described in the question. The BSSID (that you have given the link to in your answer) returns two different values for two different bands. – Urban Apr 23 '15 at 07:21
1

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

Mohit Rajput
  • 439
  • 3
  • 18
  • 1
    That doesn help. In your example, your two BSSID are the same (AA:AA:AA....), however, as I've described in the question, I'm getting two different BSSID for the same dual band router. Which means that if I have two dual band routers, I'll have 4 unique BSSID. So appending them with frequency doesn't make things any better. – Urban Apr 23 '15 at 16:14
1

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;
}
Knossos
  • 15,802
  • 10
  • 54
  • 91
  • I'm not very familiar with ARP, so I'll have to try this out, but won't this return two different address if the two frequencies are considered as two different access points? – Urban Apr 24 '15 at 15:15
  • Possibly, I don't know – Knossos Apr 24 '15 at 17:55
  • Tried it on a single band router, and it gave me the same result as the getBSSID method. I'm quite sure this will also return two addresses on a dual band router. – Urban Apr 25 '15 at 05:13