SoFunction
Updated on 2025-04-06

Android management and operation Wifi simple instance source code

Because I have been working on the network, I looked at the Wifi operation today and after sorting it out, I made a class that may not be complete, but I personally feel that it can meet the needs. Of course, the methods inside may also be wrong or incomplete. I did not conduct a complete test of this class, but only tested some of them.

In fact, it is also very simple to operate Wifi, mainly using the following objects or variables:

private WifiManager wifiManager;// Declare the management object OpenWifiprivate WifiInfo wifiInfo;// Wifi informationprivate List<ScanResult> scanResultList; // Scanned network connection listprivate List<WifiConfiguration> wifiConfigList;// Network configuration listprivate WifiLock wifiLock;// WifiLock

In fact, these can be operated without writing them into a separate class. For the sake of convenience, a unified class is written here to operate on Wifi. Through these methods, you can basically obtain all the data of operations.

The following code is for everyone, if necessary, you can use it as a reference:

package ;import ;
import ;
import ;
import ;
import ;
import ;
import ;public class WifiManageClass
{ 
private WifiManager wifiManager;// Declare management objectsprivate WifiInfo wifiInfo;// Wifi informationprivate List<ScanResult> scanResultList; // Scanned network connection listprivate List<WifiConfiguration> wifiConfigList;// Network configuration listprivate WifiLock wifiLock;// Wifi lockpublic WifiManageClass(Context context)
{ 
 = (WifiManager) context 
.getSystemService(Context.WIFI_SERVICE);// Get Wifi service// Get Wifi information = ();// Get the connection information} 
public boolean getWifiStatus() 
{ 
return (); 
} // Turn on/off wifipublic boolean openWifi() 
{ if (!()) 
{ 
return (true); 
} else { 
return false; 
} 
} 
public boolean closeWifi()
{ 
if (!())
{ 
return true; 
} 
else 
{ 
return (false); 
} 
} 
// Lock/Unlock wifi// In fact, locking WiFi is to determine whether wifi is successfully established. Here is the use of hold, which means to shake hands to acquire. Get it!public void lockWifi() 
{ 
(); 
} 
public void unLockWifi()
{ 
if (!()) { 
(); // Free up resources} 
} 
// I originally wrote it in the constructor, but considering that I don’t use Wifi locks every time, I just built a method myself!  Call it when needed, and it is OK to create itpublic void createWifiLock()
{ 
wifiLock = ("flyfly"); // Create a lock flag} // Scan the networkpublic void startScan()
{ 
(); 
scanResultList = (); // Scan to return to the result listwifiConfigList = (); // Scan the configuration list} 
public List<ScanResult> getWifiList()
{ 
return scanResultList; 
} 
public List<WifiConfiguration> getWifiConfigList()
{ 
return wifiConfigList; 
} 
// Get the scan listpublic StringBuilder lookUpscan() { 
StringBuilder scanBuilder = new StringBuilder(); 
for (int i = 0; i < (); i++) 
{ 
("serial number:" + (i + 1)); 
((i).toString()); 
//All information("\n"); 
} 
return scanBuilder; 
} //Get the strength of the specified signalpublic int getLevel(int NetId) 
{ 
return (NetId).level; 
} // Get the address of the Macpublic String getMac() { 
return (wifiInfo == null) ? "" : (); 
} 
public String getBSSID()
{ 
return (wifiInfo == null) ? null : (); 
} 
public String getSSID() 
{ 
return (wifiInfo == null) ? null : (); 
} 
// Return the ID of the currently connected networkpublic int getCurrentNetId() 
{ 
return (wifiInfo == null) ? null : (); 
} // Return all informationpublic String getwifiInfo() 
{ 
return (wifiInfo == null) ? null : (); 
} // Get the IP addresspublic int getIP() { 
return (wifiInfo == null) ? null : (); 
} // Add a connectionpublic boolean addNetWordLink(WifiConfiguration config) { 
int NetId = (config); 
return (NetId, true); 
} // Disable a link public boolean disableNetWordLick(int NetId) {(NetId); 
return (); 
} // Remove a linkpublic boolean removeNetworkLink(int NetId)
{ 
return (NetId); 
} //Do not display SSID public void hiddenSSID(int NetId){ 
(NetId).hiddenSSID=true; 
} //Show SSID public void displaySSID(int NetId){ 
(NetId).hiddenSSID=false; 
} 
}

Of course, when operating Wifi, you also need corresponding permissions. I personally feel that this is the most permissions to use. Of course, operating Wifi cannot be performed in the simulator, and must be placed on a real machine with Wifi. There are also such types of people who do not capture and deal with possible errors. I hope that friends who refer to this point will pay attention to this, otherwise they may be easily misled by sudden errors and cannot find the problem. I encountered such a problem during development! Therefore, possible problems must be dealt with accordingly!

The following are the permissions required to operate these. Of course, depending on the content of the operation, the permissions may also be different. The following permissions are for reference only:

<uses-permission android:name=".ACCESS_WIFI_STATE"></uses-permission><uses-permission android:name=".ACCESS_CHECKIN_PROPERTTES"></uses-permission><uses-permission android:name=".WAKE_LOCK"></uses-permission><uses-permission android:name=""></uses-permission><uses-permission android:name=".CHANGE_WIFI_STATE"></uses-permission><uses-permission android:name=".MODIFY_PHONE_STATE"></uses-permission> 

This is all for you to introduce the source code of simple Wifi instances. If you find any errors or incomplete in these codes, I hope to send me a message to improve it! Only by communicating with each other can we make progress!