Android Gets the device for real time connection hotspots IP

  • 2021-08-12 03:39:15
  • OfStack

Recently, many netizens have consulted this site for such a problem: by reading /proc/net/arp The file can get the IP of the device connected to the current hotspot, but the IP of the device still exists in the file after the device is disconnected. I don't know how to solve this trouble.

Just like the portable hotspot management 1 in the system setup, it can monitor the changes of the connection equipment of hotspots in real time

The following site to share 1 example code, hoping to 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 = br.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
connectedIP.add(ip);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return connectedIP;
}

Invoke the method:


ArrayList connectedIP = getConnectedIP();
resultList = new StringBuilder();
for (String ip : connectedIP) {
resultList.append(ip);
resultList.append("\n");
}
System.out.print(resultList);

PS: Let's share with you the code of Ip address of Wifi hotspot device accessed by Android

Recently, I am doing an app for transferring files between Android devices. To establish a hotspot for two devices to transfer files between the same LAN, I need to know the ip address of the device connected to the hotspot. Here, record the acquisition method under 1:


WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
    int ip = dhcpInfo.serverAddress;
    // Get here ip Is an integer type and requires conversion 
    String strIp = intToIp(ip);
  private String intToIp(int i) {
    return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
        + ((i >> 24) & 0xFF);
  }

Summarize


Related articles: