Android code settings, open Wi-Fi hotspot and hotspot connections

Source: Internet
Author: User

In fact, it is simple to create a hotspot, first get to the WiFi service, then configure the hotspot name, password and so on, and then open it through reflection OK.

Let's look at the code implementation for creating hotspots:

This section is to turn on the WLAN hotspot and can specify its hotspot name and password.

After the sub-branch, this section of the program can turn on Wi-Fi and automatically turn off WiFi.

Package Com.tel.lajoin.wifi.hotspot;import Java.lang.reflect.method;import Android.app.activity;import Android.content.context;import Android.net.wifi.wificonfiguration;import Android.net.wifi.wifimanager;import Android.os.bundle;import Android.view.view;import Android.widget.button;public class HotspotActivity extends Activity {private Wifimanager wifimanager;private Button open;private boolean flag=false; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.main);//Get WIFI Management Service Wifimanager = (Wifimanager) getsystemservice (context.wifi_service); open = (Button) Findviewbyid (r.id.open_hotspot);//Set Hotspot Open.setonclicklistener via button event (new View.onclicklistener () {@ overridepublic void OnClick (View v) {//closed if open, flag=!flag;setwifiapenabled (flag) if closed);} WiFi hotspot Switch Public boolean setwifiapenabled (Boolean enabled) {if (enabled) {//disable Wi-Fi in any case//wifi and hotspot cannot be opened simultaneously, so hit You need to close Wifiwifimana when you open hot spots.Ger.setwifienabled (false);} The configuration class of the try {//hotspot wificonfiguration apconfig = new Wificonfiguration ();//Configure the name of the hotspot (can add a random number after the name) Apconfig.ssid = "     Yrcconnection ";//configuration hotspot password apconfig.presharedkey=" 12122112 "; Set hot Spot by reflection call method = Wifimanager.getclass (). GetMethod ("setwifiapenabled", Wificonfiguration.class, Boolean.type);//return hotspot Open status return (Boolean) Method.invoke (Wifimanager, Apconfig, enabled);} catch (Exception e) {return false;}}}

 

Next in a paragraph yes, go to a Wi-Fi hotspot

Package Com.tel.lajoin.wifiscan;import Java.util.arraylist;import Java.util.list;import android.app.Activity; Import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.content.intentfilter;import Android.net.wifi.scanresult;import Android.net.wifi.wificonfiguration;import Android.net.wifi.wifimanager;import Android.os.bundle;public class Mainactivity extends Activity {private list< scanresult> wifilist;private wifimanager wifimanager;private list<string> passableHotsPot;private Wifireceiver wifireceiver;private boolean isconnected=false;private Button connect; @Overridepublic void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); init ();} /* Initialize parameter */public void init () {Setcontentview (r.layout.main); connect= (Button) Findviewbyid (r.id.connect); Wifimanager = (Wifimanager) getsystemservice (context.wifi_service); wifireceiver = new Wifireceiver ();// Search hotspot Connect.setonclicklistener via button events (new View.onclicklistEner () {@Overridepublic void OnClick (View v) {Wifimanager.startscan ();}});} /* Monitor hotspot Changes */private final class Wifireceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, in Tent Intent) {wifilist = Wifimanager.getscanresults (); if (wifilist = = NULL | | wifilist.size () = = 0 | | isconnected) return;o Nreceivenewnetworks (wifilist);}} /* When searching for a new WiFi hotspot, determine if the hotspot meets specifications */public void Onreceivenewnetworks (list<scanresult> wifilist) {passablehotspot=new Arraylist<string> (); for (Scanresult result:wifilist) {System.out.println (result. SSID), if (result. SSID). Contains ("Yrcconnection")) Passablehotspot.add (result. SSID);} Synchronized (this) {Connecttohotpot ();}} /* Connect to hotspot */public void Connecttohotpot () {if (Passablehotspot==null | | passablehotspot.size () ==0) return; Wificonfiguration wificonfig=this.setwifiparams (passablehotspot.get (0)); int wcgid = Wifimanager.addnetwork (    Wificonfig);    Boolean flag=wifimanager.enablenetwork (Wcgid, true); Isconnected=flag; System.out.println ("Connect sucCess? "+flag);} /* Set the parameters of the hotspot to connect */public wificonfiguration setwifiparams (String SSID) {wificonfiguration apconfig=new wificonfiguration (); apconfig.ssid= "\" "+ssid+" \ "" "; apconfig.presharedkey=" \ "12122112\" "apconfig.hiddenssid = True;apconfig.status = Wificonfiguration.status.enabled;apconfig.allowedgroupciphers.set (WifiConfiguration.GroupCipher.TKIP); ApConfig.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.CCMP); ApConfig.allowedKeyManagement.set ( WifiConfiguration.KeyMgmt.WPA_PSK); ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP); ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.CCMP); ApConfig.allowedProtocols.set ( WifiConfiguration.Protocol.RSN); return apconfig;} @Overrideprotected void OnDestroy () {Super.ondestroy ();/* Unregister broadcast */unregisterreceiver (wifireceiver) when destroying;}}

 

Of course, if you want to run, you have to set up the activity class defined in Manifest.xml. and the corresponding layout file and R file generation.

There are also configuration permissions.

<uses-permission android:name= "Android.permission.INTERNET" ></uses-permission><uses-permission Android:name= "Android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission> "<!--settings allow the right to change configuration information?? --      <uses-permission android:name= "Android.permission.CHANGE_CONFIGURATION"/>       < Uses-permission android:name= "Android.permission.CHANGE_NETWORK_STATE" ></uses-permission>     < Uses-permission android:name= "Android.permission.CHANGE_WIFI_STATE" ></uses-permission>     < Uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE" ></uses-permission>     < Uses-permission android:name= "Android.permission.ACCESS_WIFI_STATE" ></uses-permission>  

  

Android code settings, open Wi-Fi hotspot and hotspot connections

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.