[Java]
Package com. schogini. dhcp;
Import android. content. Context;
Import android.net. DhcpInfo;
Import android.net. wifi. WifiInfo;
Import android.net. wifi. WifiManager;
Import android. text. format. Formatter;
Public class CheckWireLess {
Private static WifiManager wifiManager;
Private static DhcpInfo dhcpInfo;
Private static WifiInfo wifiInfo;
// Obtain the ip address
Public static String getIp (Context context ){
WifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE );
DhcpInfo = wifiManager. getDhcpInfo ();
WifiInfo = wifiManager. getConnectionInfo ();
// WifiInfo returns the dynamic information of the current Wi-Fi connection
Int ip = wifiInfo. getIpAddress ();
Return "wifi_ip:" + FormatIP (ip );
}
// Obtain the Gateway
Public static String getGateWay (Context context ){
WifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE );
DhcpInfo = wifiManager. getDhcpInfo ();
// DhcpInfo obtains the last successful information, including the gateway and ip address.
Return "dh_ip:" + FormatIP (dhcpInfo. ipAddress) + "\ n" + "dh_gateway" + FormatIP (dhcpInfo. gateway );
}
// Convert the IP address to the string format
Public static String FormatIP (int IpAddress ){
Return Formatter. formatIpAddress (IpAddress );
}
}
Package com. schogini. dhcp;
Import android. content. Context;
Import android.net. DhcpInfo;
Import android.net. wifi. WifiInfo;
Import android.net. wifi. WifiManager;
Import android. text. format. Formatter;
Public class CheckWireLess {
Private static WifiManager wifiManager;
Private static DhcpInfo dhcpInfo;
Private static WifiInfo wifiInfo;
// Obtain the ip address
Public static String getIp (Context context ){
WifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE );
DhcpInfo = wifiManager. getDhcpInfo ();
WifiInfo = wifiManager. getConnectionInfo ();
// WifiInfo returns the dynamic information of the current Wi-Fi connection
Int ip = wifiInfo. getIpAddress ();
Return "wifi_ip:" + FormatIP (ip );
}
// Obtain the Gateway
Public static String getGateWay (Context context ){
WifiManager = (WifiManager) context. getSystemService (Context. WIFI_SERVICE );
DhcpInfo = wifiManager. getDhcpInfo ();
// DhcpInfo obtains the last successful information, including the gateway and ip address.
Return "dh_ip:" + FormatIP (dhcpInfo. ipAddress) + "\ n" + "dh_gateway" + FormatIP (dhcpInfo. gateway );
}
// Convert the IP address to the string format
Public static String FormatIP (int IpAddress ){
Return Formatter. formatIpAddress (IpAddress );
}
}
Therefore, you should pay attention to the selection of ip address acquisition methods when using them. The two methods are different. The above code has been tested on a local mobile phone.
Author: agods