Android control WiFi related Operations example _android

Source: Internet
Author: User
Tags stringbuffer

Android controls WiFi related operations

the full name of WiFi is wireless Fidelity, also known as the 802.11B Standard, is a high-speed wireless communication protocol, transmission speed can reach 11mb/s.

In fact, Wi-Fi does not require excessive control (when WiFi is successfully connected, you can communicate directly over the WiFi device via IP), and you typically only need to control the opening or closing of WiFi and access to some WiFi-related information (for example, MAC address, IP, and so on).

If the reader's Android phone has WiFi capabilities, you can test the example in this section on your phone. Be aware that WiFi is not tested on the Android emulator, and WiFi-related information can be obtained even on a WiFi-enabled true machine, connected to a computer or other WiFi device first.

Examples of this section can turn off and start WiFi and get a variety of WiFi-related information. First confirm that the phone is successfully connected to other WiFi devices via WiFi and then run

The example in this section shows the output information as shown in the figure.

The complete implementation code for this example is as follows:

Package Eoe.demo.wifi; 
Import java.net.Inet4Address; 
Import java.util.List; 
Import android.app.Activity; 
Import Android.content.Context; 
Import android.net.wifi.WifiConfiguration; 
Import Android.net.wifi.WifiInfo; 
Import Android.net.wifi.WifiManager; 
Import Android.os.Bundle; 
Import Android.widget.CheckBox; 
Import Android.widget.CompoundButton; 
Import Android.widget.TextView; 
 
Import Android.widget.CompoundButton.OnCheckedChangeListener; 
public class Main extends activity implements Oncheckedchangelistener {private Wifimanager wifimanager; 
Private Wifiinfo Wifiinfo; 
Private CheckBox Chkopenclosewifibox; 
Private list<wificonfiguration> wificonfigurations; 
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
Setcontentview (R.layout.main); 
Obtain Wifimanager Object Wifimanager = (Wifimanager) getsystemservice (Context.wifi_service); 
Get connection Information Object wifiinfo = Wifimanager.getconnectioninfo (); Chkopenclosewifibox = (CHECKBOx) Findviewbyid (R.id.chkopenclosewifi); 
TextView tvwificonfigurations = (TextView) Findviewbyid (r.id.tvwificonfigurations); TextView Tvwifiinfo = (TextView) Findviewbyid (r.id.tvwifiinfo); 
Chkopenclosewifibox.setoncheckedchangelistener (this); 
Sets the checkbox's checked state if (wifimanager.iswifienabled ()) {Chkopenclosewifibox.settext ("WiFi is turned on") based on the status of the current WiFi (whether it is open); 
Chkopenclosewifibox.setchecked (TRUE); 
else {chkopenclosewifibox.settext ("WiFi is Off"); 
Chkopenclosewifibox.setchecked (FALSE); 
//Get WiFi information stringbuffer sb = new StringBuffer (); 
Sb.append ("WiFi info \"); 
Sb.append ("MAC Address:" + wifiinfo.getmacaddress () + "\ n"); 
Sb.append ("Bssid of the access point:" + wifiinfo.getbssid () + "\ n"); 
Sb.append ("IP address (int):" + wifiinfo.getipaddress () + "\ n"); Sb.append ("IP address (HEX):" + integer.tohexstring (wifiinfo.getipaddress ()) + "\ n"); 
Sb.append ("IP address:" + ipinttostring (wifiinfo.getipaddress ()) + "\ n"); 
Sb.append ("Network ID:" + wifiinfo.getnetworkid () + "\ n"); 
Tvwifiinfo.settext (Sb.tostring ()); Get the configured network WificonfiguratiONS = Wifimanager.getconfigurednetworks (); 
Tvwificonfigurations.settext ("Connected wireless network \ n"); for (Wificonfiguration wificonfiguration:wificonfigurations) {Tvwificonfigurations.settext ( 
Tvwificonfigurations.gettext () + Wificonfiguration.ssid + "\ n"); 
The IP private string ipinttostring (int ip) {try {byte[] bytes = new Byte[4] that converts an int type of IP to a string; 
Bytes[0] = (byte) (0xFF & IP); 
BYTES[1] = (byte) ((0XFF00 & IP) >> 8); 
BYTES[2] = (byte) ((0xff0000 & IP) >> 16); 
BYTES[3] = (byte) ((0xff000000 & IP) >> 24); 
Return inet4address.getbyaddress (bytes). gethostaddress (); 
catch (Exception e) {return ""; Open WiFi if @Override public void oncheckedchanged (Compoundbutton buttonview, Boolean ischecked) {//Selected check box (IsC 
hecked) {wifimanager.setwifienabled (true); 
Chkopenclosewifibox.settext ("WiFi is open"); 
////Turn off WiFi else {wifimanager.setwifienabled (false) When the check box is cleared; 
Chkopenclosewifibox.settext ("WiFi is closed"); 

  } 
} 
 
}

In the Androidmanifest.xml file, use the following code to open the appropriate permissions.

xml/html code
<uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" > 
</ uses-permission> 
<uses-permission android:name= "Android.permission.WAKE_LOCK" > 
</ uses-permission> 
<uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" > 

The above is a simple example of Android WiFi source, in the Android development of the WiFi module friends can refer to.

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.