Android WiFi Development Tutorial (i) Create and close--wifi hotspots

Source: Internet
Author: User

Compared to Bluetooth,wifi, the most widely used wireless network transmission technology today, almost all smartphones, tablets and laptops support Wi-Fi internet access. Therefore, it is very necessary to master the basic WiFi development technology. This tutorial will be centered around a small demo to explore the WiFi development together with you. First on



The demo feature is simple, with four buttons, two text, and a list. Features mainly have to create WiFi hotspot, turn off WiFi hotspot, search WiFi, connect WiFi, data communication. The source code will be available at the end of the tutorial. This section focuses on the permissions that are required to create and close WiFi hotspots
 <uses-permission android:name= " Android.permission.CHANGE_NETWORK_STATE "/> < Uses-permission android:name= "android.permission.CHANGE_ Wifi_state "/> <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/ > <uses-permission android: Name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name= " Android.permission.INTERNET "/>             

Creating a WiFi hotspot
/** * Create WiFi hotspot */private void Createwifihotspot () {if (Wifimanager. iswifienabled ()) {//If WiFi is turned on, turn off WiFi, Wifimanager. setwifienabled (False); } wificonfiguration config = new Wificonfiguration (); Config. SSID = Wifi_hotspot_ssid; Config. Presharedkey ="123456789"; Config. Hiddenssid = True; Config. allowedauthalgorithms. Set (wificonfiguration. Authalgorithm. OPEN);//Open System authentication Config. allowedgroupciphers. Set (wificonfiguration. Groupcipher. TKIP); Config. allowedkeymanagement. Set (wificonfiguration. Keymgmt. WPA_PSK); Config. allowedpairwiseciphers. Set (wificonfiguration. Pairwisecipher. TKIP); Config. allowedgroupciphers. Set (wificonfiguration. Groupcipher. CCMP); Config. allowedpairwiseciphers. Set (wificonfiguration. Pairwisecipher. CCMP); Config. Status = Wificonfiguration. Status. ENABLED; Set Hotspot try {method = Wifimanager by Reflection call. GetClass (). GetMethod ( "setwifiapenabled", Wificonfiguration.class, Boolean< Span class= "Hljs-preprocessor". TYPE) .invoke ( Wifimanager, config, true)  "hotspot has turned on SSID:" + wifi_hotspot_ssid +  password:123456789 ") .settext ( "Create hotspot failed") .printstacktrace ()  textview< Span class= "Hljs-preprocessor" >.settext ( "Create hotspot failed") ;}   

Here we need to use a very important api--wifimanager. In the source code there is an introduction:
This class provides the primary API for managing all aspects of Wi-Ficonnectivity. Get an instance of this class by calling{@link android.content.Context#getSystemService(String) Context.getSystemService(Context.WIFI_SERVICE)}.

As you can see, we are able to manage WiFi connections through Wifimanager. by Context.getsystemservice (Context.wifi_service), you can get an example of it.
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    • 1
    • 1
When the hotspot is turned on, we need to make sure that WiFi is off, because most phones don't support hotspots and WiFi is turned on at the same time. Then you need to create the Wificonfiguration class, because you configure the properties of the hotspot that we want to create. What we need to pay attention to here are SSID, Presharedkey and KEYMGMT. The name, password, and encryption for the hotspot, respectively. Once the properties have been configured, we can create hotspots through the reflection mechanism of java. Wi-Fi hotspot shutdown
/** * Turn off WiFi hotspot */Publicvoidclosewifihotspot () {try {method = Wifimanager.getclass (). GetMethod ( "getwifiapconfiguration"); Method.setaccessible ( true); wificonfiguration config = (wificonfiguration) method.invoke (Wifimanager); Method method2 = Wifimanager.getclass (). GetMethod ( "setwifiapenabled", Wificonfiguration.class, boolean.class); Method2.invoke (wifimanager, config, false);} catch (nosuchmethodexception e) {e.printstacktrace ();} catch (illegalargumentexception e) {e.printstacktrace ();} catch (illegalaccessexception e) {e.printstacktrace ();} catch (invocationtargetexception e) {e.printstacktrace ();}}     
As with creating a WiFi hotspot, you also need to use Java's reflection mechanism when you close it. The last parameter is changed to False when calling the method's invoke methods. Relatively speaking, the creation and shutdown of WiFi hotspot is relatively simple. Welcome to the next Android WiFi development tutorial (ii)--wifi search and connect

Android WiFi Development Tutorial (i) Create and close--wifi hotspots

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.