Android development of Wifi basic tutorial

  • 2020-06-15 10:10:06
  • OfStack

The example of this article describes the basic knowledge of Android to develop Wifi. Share to everybody for everybody reference. The details are as follows:

Android provides the class WifiManager through which various wifi-related operations can be performed.

through


wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE) 

An instance of the class is available.

1. Get the wifi enabled status (as long as the wifi of the mobile phone is turned on, it is considered to be on, regardless of whether it is connected to an wifi) :


boolean isOpen = wifiManager.isWifiEnabled(); 

2. Modify the state of wifi to open or close wifi with parameters


wifiManager.setWifiEnabled(true); 

3. Get wifi nearby (only if wifi is already on)


List<ScanResult> list = wifiManager.getScanResults(); 

4. Get wifi on the current connection


WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 

5. Get the current wifi configuration list (i.e. the wifi list displayed on the page when the phone connects to wifi)


List<WifiConfiguration> configList = wifiManager.getConfiguredNetworks(); 

6. Connect/disconnect an wifi (note: wifi does not change its open state)

Connect, the first parameter is net id of an wifi returned in wifiManager.getConfiguredNetworks () or wifiManager.getConnectionInfo (), and the second parameter is whether to close the other wifi while connecting to this wifi


wifiManager.enableNetwork(netId, true); 

Disconnect, parameters meaning the same as above


wifiManager.disableNetwork(netId); 
wifiManager.disconnect();

Click here to download the complete code.

Hopefully this article has been helpful in your Android programming.


Related articles: