I was trying to make an app that can find the IP address of a device connected to my android phone's hotspot using it's MAC address. I know this can be done using ARP tables but I just don't know how. I'm using android studio with java and my device is running android 10. Please help.
Asked
Active
Viewed 385 times
-1
-
The question doesn't appear to include any attempt at all to solve the problem. Please edit the question to show what you've tried, and show a specific roadblock you're running into with [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask). – Andreas Oct 18 '19 at 08:28
-
The thing is, i don't have any idea how I'm supposed to do it, so I can't even try – sahil bhatnagar Oct 18 '19 at 08:49
-
Do your research first then. You know that this can be done using ARP tables, so start researching from there. StackOverflow is a site for specific programming questions and answers and as of now, your question is too broad – Andreas Oct 18 '19 at 08:50
1 Answers
0
Here's how it's implemented in AndroidNetworkTools library, which worked fine for me:
/**
* Returns all the IP/MAC address pairs currently in the ARP cache (/proc/net/arp).
*/
public static HashMap<String, String> getAllIPAndMACAddressesInARPCache() {
HashMap<String, String> macList = new HashMap<>();
for (String line : getLinesInARPCache()) {
String[] splitted = line.split(" +");
if (splitted.length >= 4) {
// Ignore values with invalid MAC addresses
if (splitted[3].matches("..:..:..:..:..:..")
&& !splitted[3].equals("00:00:00:00:00:00")) {
macList.put(splitted[0], splitted[3]);
}
}
}
return macList;
}
/**
* Method to read lines from the ARP Cache
*/
private static ArrayList<String> getLinesInARPCache() {
ArrayList<String> lines = new ArrayList<>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return lines;
}

chehonte
- 56
- 5