要知道连接到你的热点的IP地址,你可以使用以下代码示例:
public String getConnectedIP() {
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
int ip = dhcpInfo.ipAddress;
String ipAddress = Formatter.formatIpAddress(ip);
return ipAddress;
}
在代码中,我们首先获取WifiManager
实例,然后使用getDhcpInfo()
方法获取当前连接的IP地址信息。然后,我们使用Formatter.formatIpAddress()
方法将IP地址转换为字符串,并返回该字符串。
为了使用以上代码,你需要在AndroidManifest.xml文件中添加以下权限:
请确保在使用此代码之前已经连接到热点。你可以将上述代码放在你的Activity或者Fragment中的任意位置,并调用getConnectedIP()
方法来获取连接的IP地址。