stevyhacker
stevyhacker

Reputation: 1877

How to get mac addresses of devices connected to the same network in android?

How can I get mac addresses of all devices connected to the same network in android, like in this app?

Any code snippet on this would be helpful.

mac address fing

Upvotes: 4

Views: 2457

Answers (1)

Leanid Vouk
Leanid Vouk

Reputation: 498

Here is code example which accesses ARP table in Android in order to resolve IP to Mac address

string GetMACAddressviaIP(string ipAddr)
{
    string result = "";
    ostringstream ss;
    ss << "/proc/net/arp";
    string loc = ss.str();

    ifstream in(loc);
    if(!in)
    {
        printf("open %s failed\n",loc.c_str());
        return result;
    }

    string line;
    while(getline(in, line)){
        if(strstr(line.c_str(), ipAddr.c_str()))
        {
            const char *buf = strstr(line.c_str(), ":") - 2;
            int counter = 0;
            stringstream ss;
            while(counter < 17)
            {
                ss << buf[counter];
                counter++;
            }
            result = ss.str();
        }
    }

    return result;
}

Upvotes: 1

Related Questions