Problem summary (8)-Get the Wi-Fi gateway and ip address

Source: Internet
Author: User

[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
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.