SoFunction
Updated on 2025-04-09

Android obtains the device IP for real-time connection to hotspots

Recently, many netizens have consulted the editor with this question: by reading/proc/net/arpThe file can get the IP of the device connected to the current hotspot, but once the device is disconnected, the IP of the device still exists in the file. I don’t know how to solve this problem.

It is as convenient as the portable hotspot management in the system settings. It can monitor changes in hotspot connection devices in real time.

The following editor will share with you an example code, hoping it can help you. The specific code is as follows:

private ArrayList getConnectedIP() {
ArrayList connectedIP = new ArrayList();
try {
BufferedReader br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = ()) != null) {
String[] splitted = (" +");
if (splitted != null &&  >= 4) {
String ip = splitted[0];
(ip);
}
}
} catch (Exception e) {
();
}
return connectedIP;
}

Calling method:

ArrayList connectedIP = getConnectedIP();
resultList = new StringBuilder();
for (String ip : connectedIP) {
(ip);
("\n");
}
(resultList);

PS: Let me share with you a code to obtain the IP address of the WiFi hotspot device you accessed from Android

Recently, I am making an app to transfer files between Android devices. To create a hotspot, let two devices transfer files between the same local area network. You need to know the IP address of the device connecting to the hotspot. Here I will record the acquisition method:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = ();
    int ip = ;
    // Get ip as an integer type here, and it needs to be converted    String strIp = intToIp(ip);
  private String intToIp(int i) {
    return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
        + ((i >> 24) & 0xFF);
  }

Summarize

The above is the device IP for Android to obtain real-time connection hotspots that the editor introduces to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!