嗨,我是Android编程的新手.我基本上试图连接到一个接入点并发送它来命令.通过wifi连接到它后,是否可以以编程方式获取它的IP地址,以便我可以与它建立http连接?
到目前为止,我知道我们可以获得设备IP,但不确定是否可以获得接入点IP.请帮忙.提前致谢.
解决方法:
public static String getApIpAddr(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
byte[] ipAddress = convert2Bytes(dhcpInfo.serverAddress);
try {
String apIpAddr = InetAddress.getByAddress(ipAddress).getHostAddress();
return apIpAddr;
} catch (UnkNownHostException e) {
e.printstacktrace();
}
return null;
}
private static byte[] convert2Bytes(int hostAddress) {
byte[] addressBytes = { (byte)(0xff & hostAddress),
(byte)(0xff & (hostAddress >> 8)),
(byte)(0xff & (hostAddress >> 16)),
(byte)(0xff & (hostAddress >> 24)) };
return addressBytes;
}