"Android Development-smart home series" (iii): Mobile phone connection WiFi module

Source: Internet
Author: User

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

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.