如何以编程方式获取android中已连接的wifi路由器的IP地址? [英] how to obtain the ip address of the connected wifi router in android programmatically?

查看:519
本文介绍了如何以编程方式获取android中已连接的wifi路由器的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我的Android手机所连接的wifi路由器的IP地址吗?我知道我们可以使用android API来获取mac/BSSId和SSID,但是我没有找到找到IP地址的方法吗?

我找到了用于获取拥有WiFi路由器的电话的IP地址的代码

WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int ipAddress = myWifiInfo.getIpAddress();
System.out.println("WiFi address is " + android.text.format.Formatter.formatIpAddress(ipAddress))

但没有得到我想要的

解决方案

您想要的是 DhcpInfo ....

final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);

这将确定格式化的网关IP地址,该地址应该是您要查找的.

I want to obtain the ip address of the the wifi router to which my android phone is connected? I know that we can get the mac/BSSId and SSID by using the android APIS but I don't find the way to find the way to find the ip address of it?

I found the code for obtaining the ip address of phone owns wifi router

WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int ipAddress = myWifiInfo.getIpAddress();
System.out.println("WiFi address is " + android.text.format.Formatter.formatIpAddress(ipAddress))

but failed to get what I want

解决方案

What you want is DhcpInfo....

final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);

This will determine the formatted gateway IP address, which should be what you're looking for.

这篇关于如何以编程方式获取android中已连接的wifi路由器的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆