Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Directory (?) [+]
Overview
To achieve the ability to connect to the WiFi will use a tool class, source code can click the link to download. The code in these similar tools on the Web is pretty much the same. There are two main ways to connect to a wireless network:
There is a connect method, there is a Connectspecificap method, I use the latter for a WiFi module that does not require a password to link.
Main steps
Connecting WiFi is roughly divided into several steps:
"1" Turn on WiFi
Openwifi
"2" Configure Network information
Createwifiinfo return Wificonfig
"3" Add a configured network and connect
int NetID = Mwifimanager.addnetwork (wificonfig);
Boolean bRet = Mwifimanager.enablenetwork (NetID, false);
"4" Determines if the connection is successful
The above Enablenetwork method returns success does not reflect whether the phone is actually connected successfully, so also need to call the Isconnect method to judge, for their own business logic and scene, I wrote a method to determine whether the connection is successful: Iswificonnected method
Application
/** * Mobile Access Module WiFi network--blackants */PrivatevoidConnectdisplay () {Get Message Object Msg=mainhandler.obtainmessage ();Whether the connection succeeds with a token of Boolean isconnected=FalseFlag Blackants Scanresult exists, true exists, false does not exist Boolean isexist=FalseThe user holds blackants WiFi information scanresult srwifiinfo=NullDeclares a variable String strtempssid for temporary storage of the SSID;/****************** The following piece of code is primarily to get Blackants's Scanresult (including its ssid,bssid,capabilities) *****************/Scan WiFi wifiadmin.startscan ();Get WiFi list list<scanresult> lstwifi= wifiadmin.getwifilist ();If the WiFi list is empty, the WiFi switch is not turned on, sending a message to handlerif (lstwifi==null | | Lstwifi.size () = =0) {msg.what=notopen; Mainhandler.sendmessage (msg);Return }If the list exists, the list is traversedif (lstwifi!=Null & Lstwifi.size () >0) {Traverse the list to see if blackants existsForint i=0;i<lstwifi.size (); i++) {Strtempssid=lstwifi.get (i). SSID;//if present, Exit For loop if ((Displayssid). Equals (Strtempssid.trim ( ) {//modify the flag bit for the presence of Isexist=true; //blackants WiFi information into the variable srwifiinfo srwifiinfo=lstwifi.get (i); break;} }} /****************** the above code is mainly to get blackants Scanresult (including its ssid,bssid,capabilities ) *****************///default is failure msg.what=failure; //if present, let the phone access blackants-------------------V1.0 if (isexist) {if (Wifiadmin.connectspecificap (srwifiinfo)) {//determine if the connection is Span class= "Hljs-keyword" >if (wifiadmin.iswificonnected (Initactivity.
Report:
Download Tool Class (Wifiadmin)
"Android Development-smart home series" (iii): Mobile phone connection WiFi module