Android如何实时获取到连接热点的设备IP

通过读取/proc/net/arp文件可以得到连接当前热点的设备的IP,但是一旦设备断开后,该设备的IP还是存在该文件中,求各路大神的解决方案

就像系统设置里面的 便携式热点管理一样的方便 的方式 可以实时的监控热点的连接设备的变化

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;
}

调用方法:

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

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
int ip = dhcpInfo.serverAddress;
//此处获取ip为整数类型,需要进行转换
String strIp = intToIp(ip);

private String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
+ ((i >> 24) & 0xFF);
}

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
int ip = dhcpInfo.serverAddress;
//此处获取ip为整数类型,需要进行转换
String strIp = intToIp(ip);

private String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."

  • ((i >> 24) & 0xFF); }

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
int ip = dhcpInfo.serverAddress;
//此处获取ip为整数类型,需要进行转换
String strIp = intToIp(ip);

private String intToIp(int i) {
return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."

  • ((i >> 24) & 0xFF); }