Move-android-wi-fi tutorial[]

Source: Internet
Author: User

Http://www.tutorialspoint.com/android/android_wi_fi.htm

Android allows applications to access-view the the state of the wireless connections at very low level. Application can access almost all the information of a WiFi connection.

The information that an application can access includes connected network's link speed,ip address, n Egotiation State (negotiation status), other networks information. Applications can also scan, add, save, terminate and initiate Wi-Fi connections.

Android provides Wifimanager API to manage all aspects (aspect) of the WIFI connectivity. We can instantiate this class by calling Getsystemservice method. Its syntax is given below−

In order to scan a list of wireless networks, the also need to register your broadcastreceiver. It can be registered using Registerreceiver method with argument of your receiver class object. Its syntax is given below−

extends Broadcastreceiver {   publicvoid  onreceive (Context C, Intent Intent) {    New  New intentfilter (wifimanager.scan_results_available_action));  

The WiFi scan can be-start by calling the Startscan method of the Wifimanager class. This method returns a list of Scanresult objects. You can access any object by calling the Get method of list. Its syntax is given below−

list<scanresult> wifiscanlist == wifiscanlist.get (0). toString ();
In general, the network environment is not how to change, try to reduce the number of WiFi scanning, I wrote the program when I found that each scan of about 10 seconds to complete ...

In addition, some WiFi scan will be triggered when the mobile WiFi is disconnected.

Below is a modified WiFi riceiver that uses handler to propagate messages

 PackageCom.example.longer3d.wifi;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.net.NetworkInfo;Importandroid.net.wifi.SupplicantState;ImportAndroid.net.wifi.WifiInfo;ImportAndroid.net.wifi.WifiManager;ImportAndroid.os.Handler;ImportAndroid.util.Log; Public classWifistatusreceiverextendsBroadcastreceiver {/*** WiFi connection is successful*/     Public Static Final intWifi_handler_connect_success = 0x101; /*** WiFi Connection failed*/     Public Static Final intWifi_handler_connect_faild = 0x102; /*** WiFi connection failed, password is wrong, but only occasionally catch =_=. */     Public Static Final intWifi_handler_connect_faild_auth = 0x103; PrivateHandler Handler;  Publicwifistatusreceiver (Handler Handler) { This. Handler =handler; } @Override Public voidOnReceive (Context context, Intent Intent) {String action=intent.getaction (); Switch(action) { Casewifimanager.supplicant_state_changed_action://            //View Source Supplicantstatetracker.java//Supplicantstate supl_state =( (supplicantstate) Intent.getparcelableextra (wifimanager.extra_new_state)); if(Supl_state = =supplicantstate.completed) {//Add callbackhandler.sendemptymessage (wifi_handler_connect_success); LOG.E ("Supplicantstate", "WiFi completed"); } Else if(Supl_state = =supplicantstate.disconnected) {handler.sendemptymessage (wifi_handler_connect_faild); LOG.E ("Supplicantstate", "WiFi Disconnected"); } Else{LOG.E ("Supplicantstate", String.valueof (supl_state)); LOG.E ("Supplicantstate", "WiFi Unknown"); }            if(Intent.hasextra (wifimanager.extra_supplicant_error)) {Handler.sendemptymessage (wifi_handler_connect_                Faild_auth); LOG.E ("Supplicantstate", "WiFi Authentication failed!" "); }             Break; //WiFi connection on or off//will be called faster, and many times, do not know why, first do not use         Casewifimanager.network_state_changed_action://LOG.E ("receiver", "Network status Change");Networkinfo info =Intent.getparcelableextra (Wifimanager.extra_network_info); if(Info.getstate (). Equals (NetworkInfo.State.DISCONNECTED)) {//handler.sendemptymessage (wifi_handler_connect_faild);LOG.E ("receiver", "WiFi network connection Disconnected"); } Else if(Info.getstate (). Equals (NetworkInfo.State.CONNECTED)) {//get the current WiFi nameWifimanager Wifimanager =(Wifimanager) Context.getsystemservice (Context.wifi_service); Wifiinfo Wifiinfo=Wifimanager.getconnectioninfo (); LOG.E ("Receiver", "Connect to Network" +Wifiinfo.getssid ()); //Add callback//handler.sendemptymessage (wifi_handler_connect_success);            }             Break; //WiFi Open or not         Casewifimanager.wifi_state_changed_action:intWifistate =Intent.getintextra (Wifimanager.extra_wifi_state, wifimanager.wifi_state_disabled); if(Wifistate = =wifimanager.wifi_state_disabled) {LOG.E ("Receiver", "system off WiFi"); } Else if(Wifistate = =wifimanager.wifi_state_enabled) {LOG.E ("Receiver", "System on WiFi"); }             Break; }    }}

Move-android-wi-fi tutorial[]

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.