Android 中怎么获取连接热点的ip

发布时间:2021-06-28 18:07:39 作者:Leah
来源:亿速云 阅读:567

Android 中怎么获取连接热点的ip,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

具体代码如下所示:

WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
System.out.println("=================");
wifiManager.setWifiEnabled(true);
}
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String IPAddress = intToIp(wifiInfo.getIpAddress());
System.out.println("IPAddress-->>" + IPAddress);
DhcpInfo dhcpinfo = wifiManager.getDhcpInfo();
String serverAddress = intToIp(dhcpinfo.serverAddress);
System.out.println("serverAddress-->>" + serverAddress);

其中IPAddress 是本机的IP地址,serverAddress 是你所连接的wifi热点对应的IP地址 

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

当在Android设备终端上使用Wifi热点的时候,需要获知Wifi热点的运行状态,热点是否打开,连接到该WIFI热点的设备数量,以及连接设备的具体IP和MAC地址。

使用re文件管理器去"/proc/net/arp",打开,发现连接上热点的设备信息都在这里了,包括mac ip等。

鉴于此,我们可以在代码中打开该文件,并获取WIFI热点的信息。

获取WIFI热点状态的方法getWifiApState()和判断热点是否可用的方法isApEnabled(),在Android源码WifiManager.Java中已经实现,但是它们是Hide方法,在SDK层面是不能访问的,如要访问需要用到java反射的机制。具体代码实现如下:

其中定义WIFI AP的几个状态

public static final int WIFI_AP_STATE_DISABLING = 10;  
public static final int WIFI_AP_STATE_DISABLED = 11;  
public static final int WIFI_AP_STATE_ENABLING = 12;  
public static final int WIFI_AP_STATE_ENABLED = 13;  
public static final int WIFI_AP_STATE_FAILED = 14;

对应于WifiMangaer.java中对这几个状态的定义。 

获取WIFI热点的状态:

public int getWifiApState(Context mContext) {  
  WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);  
    try {  
      Method method = wifiManager.getClass().getMethod("getWifiApState");  
      int i = (Integer) method.invoke(wifiManager);  
      Log.i(TAG,"wifi state: " + i);  
      return i;  
    } catch (Exception e) {  
      Log.e(TAG,"Cannot get WiFi AP state" + e);  
      return WIFI_AP_STATE_FAILED;  
    }  
  }

判断Wifi热点是否可用: 

public boolean isApEnabled(Context mContext) {  
    int state = getWifiApState(mContext);  
    return WIFI_AP_STATE_ENABLING == state || WIFI_AP_STATE_ENABLED == state;  
  }

获取链接到当前热点的设备IP: 

private ArrayList<String> getConnectedHotIP() { 
  ArrayList<String> connectedIP = new ArrayList<String>(); 
  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; 
} 
 
//输出链接到当前设备的IP地址 
public void printHotIp() { 
  ArrayList<String> connectedIP = getConnectedHotIP(); 
  StringBuilder resultList = new StringBuilder(); 
  for (String ip : connectedIP) { 
    resultList.append(ip); 
    resultList.append("\n"); 
  } 
  System.out.print(resultList); 
  Log.d(TAG,"---->>heww resultList="+resultList); 
}

当然在应用中要添加访问WIFI设备的权限: 

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

看完上述内容,你们掌握Android 中怎么获取连接热点的ip的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Android Socket 发送广播包的那些坑
  2. android 上层wifi模块调用分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:android中怎么判断网络是否可用

下一篇:Android中怎么在程序运行时申请权限

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》