首页 > 系统 > Android > 正文

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

2019-10-22 18:16:12
字体:
来源:转载
供稿:网友

最近有很多网友向小编咨询这样的问题:通过读取/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);

PS:下面再给大家分享一段 安卓获取接入的Wifi热点设备的Ip地址的代码

最近在做一个安卓设备间传输文件的app,建立热点让两部设备在同一个局域网之间传输文件,需要知道连接热点的设备的ip地址,这边记录一下获取的方式:

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

总结

以上所述是小编给大家介绍的Android获取实时连接热点的设备IP,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对VEVB武林网网站的支持!


注:相关教程知识阅读请移步到Android开发频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表