Can I get the IP address of the access point to connect to Wi-Fi?

Hi, I am new to Android programming. I am basically trying to connect to an access point and send incoming commands. After connecting to it via Wi-Fi, is it possible to programmatically obtain its IP address so that I can establish an http connection with it? So far I know that we can get the IP address of the device, but are not sure that the IP address of the access point can be obtained. Please help. Thanks in advance.

+4
source share
3 answers
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; } 
+3
source

I assume that you mean the external (public) IP address of the access point to which the device is connected. If yes, yes, there is an easy way to get the public IP address of the access point to which the device is connected. Just set up a script on a web server that will repeat the IP address of any client that connects to it (similar to www.whatismyip.com). Then your device just needs to make a GET request to the script, and this will return the external IP address of the access point to which the device is connected.

0
source

I use this to get the IP address

 try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { //My IP address String Ip= inetAddress.getHostAddress().toString(); } } } } catch (SocketException e) { Log.e("Error occurred ", e.toString()); } 
0
source

Source: https://habr.com/ru/post/1492819/


All Articles