最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Android获取热点主机ip和连接热点手机ip的教程
时间:2022-06-25 23:04:11 编辑:袖梨 来源:一聚教程网
有许多初学android开发的朋友不知道Android怎么获取热点主机ip和连接热点手机ip,小编将这篇文章分享给大家,希望能对大家有所帮助
其中定义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热点是否可用:
private ArrayListgetConnectedHotIP() { 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);
相关文章
- dnf神界版本蓝拳装备搭配毕业攻略 神界版本蓝拳装备怎么选 11-29
- 流放之路S24速刷简单策略选择推荐 11-29
- 魔兽世界乌龟服防骑a怪一键宏指令分享 11-29
- 创世秩序全地图资料汇总 全地图区域中文名字对照 11-29
- 播种委员会全角色解锁攻略 全人物解锁位置详解 11-29
- 魔兽世界N服官网地址分享 Everlook服入口位置2024 11-29