Is there any way to get a the list of connected MAC addresses when my phone is on Wi-Fi tethering mode?
Asked
Active
Viewed 2.0k times
6
-
Please check this link. http://stackoverflow.com/questions/21522961/how-to-get-the-client-device-details-which-is-connected-to-wifi-hotspot – arjun Mar 14 '14 at 09:41
4 Answers
9
Firstly, you must have a rooted device. When it's done just read dnsmasq.leases file. Usually it is placed at: /data/misc/dhcp/dnsmasq.leases. A structure of the file is pretty simple - each line is a summary of a connected user. The summary has several fields including MAC. I didn't find a possibility to get MAC without root. Please correct me if I'm wrong.

Pavel
- 106
- 1
-
1what about clients connected with static ip not dhcp, they will not appear in the dnsmasq, do you have any idea? – user935143 Feb 18 '13 at 07:28
-
7
Reading /proc/net/arp
would provide the information for both static and DHCP clients that have communicated with the device during the last 60 seconds (configured in /proc/sys/net/ipv4/neigh/wl0.1/gc_stale_time
where wl0.1
is the wireless network interface on my phone).
It is available for non-root users too.

Roman
- 171
- 1
- 3
-
2Is that true under /proc/net/arp is not always updating the list when the device (client) disconnected from the AP @Roman? – gumuruh Aug 08 '14 at 17:53
1
@SuppressWarnings("ConstantConditions")
public static String getClientMacByIP(String ip)
{
String res = "";
if (ip == null)
return res;
String flushCmd = "sh ip -s -s neigh flush all";
Runtime runtime = Runtime.getRuntime();
try
{
runtime.exec(flushCmd,null,new File("/proc/net"));
}
BufferedReader br;
try
{
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null)
{
String[] sp = line.split(" +");
if (sp.length >= 4 && ip.equals(sp[0]))
{Assistance.Log(sp[0]+sp[2]+sp[3],ALERT_STATES.ALERT_STATE_LOG);
String mac = sp[3];
if (mac.matches("..:..:..:..:..:..") && sp[2].equals("0x2"))
{
res = mac;
break;
}
}
}
br.close();
}
catch (Exception e)
{}
return res;
}
//--------------------------------------------------------
@SuppressWarnings("ConstantConditions")
public static String getClientIPByMac(String mac)
{
String res = "";
if (mac == null)
return res;
String flushCmd = "sh ip -s -s neigh flush all";
Runtime runtime = Runtime.getRuntime();
try
{
runtime.exec(flushCmd,null,new File("/proc/net"));
}
BufferedReader br;
try
{
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null)
{
String[] sp = line.split(" +");
if (sp.length >= 4 && mac.equals(sp[3]))
{
String ip = sp[0];
if (ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}") && sp[2].equals("0x2"))
{
res = ip;
break;
}
}
}
br.close();
}
catch (Exception e)
{}
return res;
}

Ali Bagheri
- 3,068
- 27
- 28
-
While there might be some problems with this answer from the standpoint of the "sh ip -s -s neigh flush all" command, it leads one in the right direction. If connected, /proc/net/arp should have the current neighbords in the file, and this file is accessible from non-rooted devices, making this solution viable to read this file, and parse off the address of the p2p-wlan0-0 device, which on my devices always is in the 192.168.49.* address range. – truedat101 Jul 07 '18 at 21:46
0
public static ArrayList<String> getConnectedDevicesMac()
{
ArrayList<String> res = new ArrayList<String>();
//NetManager.updateArpFile();
BufferedReader br;
try
{
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
line = br.readLine();
while ((line = br.readLine()) != null)
{
String[] sp = line.split(" +");
if (sp[3].matches("..:..:..:..:..:.."))
res.add(sp[3]);
}
br.close();
}
catch (Exception e)
{}
return res;
}

Ali Bagheri
- 3,068
- 27
- 28
-
Please provide some additional info on what your code is doing. It's hard to tell for anyone new what's going on. – Kurt Van den Branden Nov 12 '16 at 18:53