Android获取热点主机ip和连接热点手机ip的代码

其中定义WIFI AP的几个状态

创新互联公司2013年成立,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元福清做网站,已为上家服务,为福清各地企业和个人服务,联系电话:18980820575

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热点是否可用:

private ArrayList getConnectedHotIP() { 
  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; 
} 
//输出链接到当前设备的IP地址 
public void printHotIp() { 

  ArrayList 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设备的权限:

 

获取热点主机ip地址的代码:

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      DhcpInfo info=wifiManager.getDhcpInfo();
      System.out.println(info.serverAddress);

总结

以上所述是小编给大家介绍的Android获取热点主机ip和连接热点手机ip的方法 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!


文章题目:Android获取热点主机ip和连接热点手机ip的代码
文章出自:http://azwzsj.com/article/ihocso.html