In Android, all WiFi operations are performed in android.net. wiFi bag, And the commonly used classes only use a few. For general developers, the commonly used operations are also limited and do not need to be deliberately remembered, when I use it, I can check it now. The following is an example of WiFi in the secrets of Android Application Development. I personally feel that it is quite complete. I 'd like to share it with you!
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
package com.yarin.android.Examples_08_08; import java.util.List; import android.content.Context; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiManager.WifiLock; public class WifiAdmin { // Define the wifimanager object
private
WifiManager mWifiManager; // Define the wifiinfo object
private
WifiInfo mWifiInfo; // List of scanned Network Connections
private
List<ScanResult> mWifiList; // Network connection list
private
List<WifiConfiguration> mWifiConfiguration; // Define a wifilock
WifiLock mWifiLock;
// Constructor
public
WifiAdmin(Context context) {
// Obtain the wifimanager object
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
// Obtain the wifiinfo object
mWifiInfo = mWifiManager.getConnectionInfo();
}
// Enable WiFi
public
void OpenWifi() {
if
(!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
// Disable WiFi
public
void CloseWifi() {
if
(!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
// Lock wifilock
public
void AcquireWifiLock()
{
mWifiLock.acquire();
}
// Unlock wifilock
public
void ReleaseWifiLock()
{
// Lock upon judgment
if
(mWifiLock.isHeld()) {
mWifiLock.acquire();
}
}
// Create a wifilock
public
void CreatWifiLock() {
mWifiLock = mWifiManager.createWifiLock("Test");
}
// Obtain the configured network
public
List<WifiConfiguration> GetConfiguration() {
return
mWifiConfiguration; }
// Specify the configured network for connection
public
void ConnectConfiguration(int
index) {
// Returns an index larger than the configured network index
if(index > mWifiConfiguration.size())
{
return;
}
// Connect the network with the specified ID
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true); }
public
void StartScan() {
mWifiManager.startScan();
// Obtain the scan result
mWifiList = mWifiManager.getScanResults();
// Obtain the configured network connection
mWifiConfiguration = mWifiManager.getConfiguredNetworks();
}
// Obtain the Network List
public
List<ScanResult> GetWifiList() {
return
mWifiList; }
// View scan results
public
StringBuilder LookUpScan() {
StringBuilder stringBuilder =
new StringBuilder();
for
(int i = 0; i < mWifiList.size(); i++)
{
stringBuilder.append("Index_"+new
Integer(i + 1).toString() +
":"); // Convert scanresult information into a string package
// Include bssid, SSID, capabilities, frequency, and level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("/n");
}
return
stringBuilder; }
// Obtain the MAC address
public
String GetMacAddress() {
return
(mWifiInfo == null) ?
"NULL" : mWifiInfo.getMacAddress();
}
// Obtain the bssid of the Access Point
public
String GetBSSID() {
return
(mWifiInfo == null) ?
"NULL" : mWifiInfo.getBSSID();
}
// Obtain the IP address
public
int GetIPAddress() {
return
(mWifiInfo == null) ?
0 : mWifiInfo.getIpAddress();
}
// Obtain the connection ID
public
int GetNetworkId() {
return
(mWifiInfo == null) ?
0 : mWifiInfo.getNetworkId();
}
// Obtain all information packages of wifiinfo
public
String GetWifiInfo() {
return
(mWifiInfo == null) ?
"NULL" : mWifiInfo.toString();
}
// Add a network and connect
public
void AddNetwork(WifiConfiguration wcg)
{
int
wcgID = mWifiManager.addNetwork(wcg); mWifiManager.enableNetwork(wcgID,
true); }
// Disconnect the network with the specified ID
public
void DisconnectWifi(int
netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
} |
With these methods, I think you can refer to general WiFi operations.