How to control WiFi in Android test

Source: Internet
Author: User
Tags get ip bssid

Some cases need to control the network to verify when Robotium writing automated test case, this time need to turn back and forth, turn on the WiFi connection. Search on the Internet, the use of the method is relatively simple, organized as follows:

1, first need to apply for WiFi access in the Androidmanifest.xml file in the app, configured as follows:

XML code
  1. <!--request permission to operate WiFi--
  2. <uses-permission android:name="Android.permission.ACCESS_WIFI_STATE"></ Uses-permission>
  3. <uses-permission android:name="Android.permission.CHANGE_WIFI_STATE"></ Uses-permission>
  4. <uses-permission android:name="Android.permission.WAKE_LOCK"></ Uses-permission>

2. The Java WiFi Control code is as follows:

  1. Importjava.util.List; ImportAndroid.content.Context; ImportAndroid.net.wifi.ScanResult; Importandroid.net.wifi.WifiConfiguration; ImportAndroid.net.wifi.WifiInfo; ImportAndroid.net.wifi.WifiManager; ImportAndroid.net.wifi.WifiManager.WifiLock;  Public classwifiadmin{//code from the network, the site can not find the ...    PrivateWifimanager Mwifimanager;//Defining Wifimanager Objects      PrivateWifiinfo Mwifiinfo;//Defining Wifiinfo Objects      PrivateList<scanresult> mwifilist;//List of scanned network Connections      PrivateList<wificonfiguration> mwificonfiguration;//List of network ConnectionsWifilock Mwifilock;//Define a Wifilock       PublicWifiadmin (Context context) {Mwifimanager= (Wifimanager) context.getsystemservice (Context.wifi_service);//Get Wifimanager ObjectMwifiinfo = Mwifimanager.getconnectioninfo ();//Get Wifiinfo Object    }      //Turn on WiFi     Public voidOpenwifi () {if(!mwifimanager.iswifienabled ()) {mwifimanager.setwifienabled (true); }      }      //Turn off WiFi     Public voidClosewifi () {if(mwifimanager.iswifienabled ()) {mwifimanager.setwifienabled (false); }      }      //lock Wifilock, need to be locked when downloading large files     Public voidAcquirewifilock () {mwifilock.acquire (); }             Public voidReleasewifilock () {//Unlock Wifilock//determine when to lock        if(Mwifilock.isheld ()) {mwifilock.acquire (); }      }             Public voidCreatwifilock () {//Create a WifilockMwifilock = Mwifimanager.createwifilock ("Test"); }         PublicList<wificonfiguration> GetConfiguration () {//get a configured network        returnmwificonfiguration; }             Public voidConnectconfiguration (intIndex) {//specify a configured network to connect//index is larger than the configured network index returns        if(Index >mwificonfiguration.size ()) {              return; }          //connect the configured network with the specified IDMwifimanager.enablenetwork (Mwificonfiguration.get (index). Networkid,true); }             Public voidStartscan () {Mwifimanager.startscan (); //Get scan ResultsMwifilist =mwifimanager.getscanresults (); Mwificonfiguration= Mwifimanager.getconfigurednetworks ();//get a configured network connection    }             PublicList<scanresult> Getwifilist () {//get a list of networks        returnmwifilist; }             PublicStringBuilder Lookupscan () {//View Scan ResultsStringBuilder StringBuilder =NewStringBuilder ();  for(inti = 0; I < mwifilist.size (); i++) {stringbuilder.append ("Index_" +NewInteger (i + 1). ToString () + ":"); //converting Scanresult information into a string package//These include: BSSID, SSID, capabilities, frequency, levelStringbuilder.append ((Mwifilist.get (i)). ToString ()); Stringbuilder.append ("\ n"); }          returnStringBuilder; }            //Get MAC Address     PublicString getmacaddress () {return(Mwifiinfo = =NULL) ? "NULL": Mwifiinfo.getmacaddress (); }            //get the Bssid of the access point     PublicString Getbssid () {return(Mwifiinfo = =NULL) ? "NULL": Mwifiinfo.getbssid (); }            //Get IP Address     Public intgetipaddress () {return(Mwifiinfo = =NULL) ? 0: Mwifiinfo.getipaddress (); }            //get the ID of the connection     Public intGetnetworkid () {return(Mwifiinfo = =NULL) ? 0: Mwifiinfo.getnetworkid (); }      //get all the Wifiinfo packets     PublicString Getwifiinfo () {return(Mwifiinfo = =NULL) ? "NULL": Mwifiinfo.tostring (); }            //Add a network and connect     Public voidaddnetwork (wificonfiguration WCG) {intWcgid =mwifimanager.addnetwork (WCG); Mwifimanager.enablenetwork (Wcgid,true); }      //Disconnect the network with the specified ID     Public voidDisconnectwifi (intnetId)          {mwifimanager.disablenetwork (netId);      Mwifimanager.disconnect (); }  }   

How to control WiFi in Android test

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.