Android提供了android.net.wifi包供我们操作WiFi。首先介绍一下几个类:
WifiManager:提供了管理WiFI连接的大部分API
ScanResult:已经检测出的接入点(包括接入点的地址、名称、身份认证、频率、信号强度)
WifiConfiguration:WiFi连接的网络配置(包括安全配置等)
WifiInfo:WiFI无线连接的描述(包括接入点、网络连接状态、隐藏的接入点、IP地址、连接速度、MAC地址、网络ID、信号强度等等)
WifiManager.WifiLock:允许应用程序一直使用WiFI无线网络,通常情况下当用户在一段时间内没有任何操作的时候,WiFi网络会自动关闭。我们使用WifiLock来锁定WiFi网络,使其一直保持连接,直到这个锁被释放。多个应用程序可能有多个锁,当多有的应用程序的锁都被释放的时候,WiFi才被关闭。
下面我们通过代码理解WiFI的使用:
通过以下代码获得WifiManager对象(Wifi是系统的一项服务)
[java]
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);打开WiFI[java] view plaincopyprint?public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}关闭WiFi[java] view plaincopyprint?public void closeWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
public void closeWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}创建一个WifiLock[java] view plaincopyprint?public void creatWifiLock() {
mWifiLock = mWifiManager.createWifiLock("Test");
}
public void creatWifiLock() {
mWifiLock = mWifiManager.createWifiLock("Test");
}锁定WifiLock[java] view plaincopyprint?public void acquireWifiLock() {
mWifiLock.acquire();
}
public void acquireWifiLock() {
mWifiLock.acquire();
}释放WifiLock[java] view plaincopyprint?public void releaseWifiLock() {
if (mWifiLock.isHeld()) {
mWifiLock.release();
}
}
public void releaseWifiLock() {
if (mWifiLock.isHeld()) {
mWifiLock.release();
}
}添加一个WiFi并连接[java] view plaincopyprint?public void addNetworkAndConn(WifiConfiguration wcg) {
int netId = mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(netId, true);
}
public void addNetworkAndConn(WifiConfiguration wcg) {
int netId = mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(netId, true);
}断开一个指定ID的WiFi[java] view plaincopyprint?public void disconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
public void disconnectWifi(int netId) {
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}连接到一个已经配置好了的WiFi[html] view plaincopyprint?public void connectConfiguration(int index) {
// 索引大于配置好的网络索引返回
if (index > mWifiConfiguration.size()) {
return;
}
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true);
}
public void connectConfiguration(int index) {
// 索引大于配置好的网络索引返回
if (index > mWifiConfiguration.size()) {
return;
}
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
true);
}扫描可接入的WiFi
[java]
public void startScan() {
mWifiManager.startScan();
// 得到扫描结果
List<ScanResult> wifiList = mWifiManager.getScanResults();
// 得到配置好的网络连接
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
}
public void startScan() {
mWifiManager.startScan();
// 得到扫描结果
List<ScanResult> wifiList = mWifiManager.getScanResults();
// 得到配置好的网络连接
List<WifiConfiguration> wifiConfigList = mWifiManager.getConfiguredNetworks();
}查看扫描结果
[java]
public StringBuilder lookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++) {
stringBuilder
.append("Index_" + new Integer(i + 1).toString() + ":");
// 将ScanResult信息转换成一个字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("\n");
}
return stringBuilder;
}
public StringBuilder lookUpScan() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++) {
stringBuilder
.append("Index_" + new Integer(i + 1).toString() + ":");
// 将ScanResult信息转换成一个字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("\n");
}
return stringBuilder;
}得到WiFi的一些信息[java] view plaincopyprint?WifiInfo mifiInfo = mWifiManager.getConnectionInfo();
// 得到MAC地址 www.zzzyk.com
public String GetMacAddress() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
}
// 得到接入点的BSSID
public String GetBSSID() {
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
}
// 得到IP地址
public int GetIPAddress() {
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}