标签:
转自:http://www.th7.cn/Program/Android/201303/130087.shtml
android本身已经提供了包来处理Android环境下对Wifi的操作,位置是android.net.wifi下面。调用wifi操作需要在mainfest中添加对应权限。
官方提供的大致可以分为四个主要的类:WifiManager,ScanResult,WifiConfiguration,WifiInfo,此外还有WifiLock,MulticastLock等
提供wifi管理的各种主要API,主要包含wifi的扫描,建立连接,配置等
http://developer.android.com/reference/android/net/wifi/WifiManager.html
描述开启扫描后的Wifi热点的信息。包含SSID、Capabilities、frequency、level(信号强度)等。
http://developer.android.com/reference/android/net/wifi/ScanResult.html
描述wifi的连接信息,包含SSID、SSID隐藏、Password等的设置。
http://developer.android.com/reference/android/net/wifi/WifiConfiguration.html
描述已建立连接后的wifi信息。包含ip、mac地址、连接速度等信息。区别与ScanResult的概念。
http://developer.android.com/reference/android/net/wifi/WifiInfo.html
WifiManager wm = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
bRet = wifiManager.isWifiEnabled();//判断是否开启
bRet = wifiManager.setWifiEnabled(true);//设置开启关闭
注意,调用setWifiEnabled后,系统进行wifi模块的开启需要一定时间,此时通过wifiManager.getWifiState()获取的状态来判断是否完成。
WifiManager.WIFI_STATE_DISABLED : WIFI网卡不可用(1)
WifiManager.WIFI_STATE_DISABLING : WIFI网卡正在关闭(0)
WifiManager.WIFI_STATE_ENABLED : WIFI网卡可用(3)
WifiManager.WIFI_STATE_ENABLING : WIFI网正在打开(2) (WIFI启动需要一段时间)
WifiManager.WIFI_STATE_UNKNOWN : 未知网卡状态
while (wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
try {
Thread.currentThread();
Thread.sleep(100);
} catch (InterruptedException ie) {
}
}
List<ScanResult> list = null;if ( wifiManager.startScan())list = wifiManager.getScanResults();
scanResult.level //信号强度scanResult.frequency //频率
List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();//获取保存的配置信息
for (WifiConfiguration existingConfig : existingConfigs) {
if (existingConfig.SSID.equals("/"" + SSID + "/"")) {
return existingConfig;
}
}
wifiManager.disableNetwork(existingConfig.networkId);
wifiManager.removeNetwork(networkId);
注意与上面的disableNetwork的区别。Disable只是单纯的断开连接,保存的ssid和密码并不清除。
Remove则是从手机中移除(忘记)该连接配置,用来清除保存的密码
在网络正常连接的状态,也可直接调用remove来代替disable中断网络,但会清除密码信息。
public WifiConfiguration CreateWifiConfig(String SSID, String Password, WifiCipherType Type) {
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "/"" + SSID + "/"";
//无密码
if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
//WEP加密
if (Type == WifiCipherType.WIFICIPHER_WEP) {
config.preSharedKey = "/"" + Password + "/"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} //WPA加密
if (Type == WifiCipherType.WIFICIPHER_WPA) {
config.preSharedKey = "/"" + Password + "/"";
config.hiddenSSID = true;
// config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
// config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
// config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
// config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
// config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
// config.status = WifiConfiguration.Status.ENABLED;
} else {
return null;
}
return config;
}
public boolean addNewWifi(WifiConfiguration newConfig){
int netID = wifiManager.addNetwork(newConfig);//添加
boolean bRet = wifiManager.enableNetwork(netID, false);//启动
return bRet;
}
WifiInfo wInfo = wifiManager.getConnectionInfo();//获取当前连接的info
wInfo.getBSSID();// 获取BSSID
wInfo.getHiddenSSID();// 获得SSID 是否被隐藏
wInfo.getIpAddress();// 获取IP 地址
wInfo.getLinkSpeed();// 获得连接的速度
wInfo.getMacAddress();// 获得Mac 地址
wInfo.getRssi();// 获得802.11n 网络的信号
wInfo.getSSID();// 获得SSID
SupplicantState suppState = wInfo.getSupplicantState();// 返回具体客户端状态的信息
wInfo.getDetailedStateOf(suppState);// 获取客户端的连通性
修改网络状态的权限
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" ></uses-permission>
修改WIFI状态的权限<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" ></uses-permission>
访问网络权限<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" ></uses-permission>
访问WIFI权限<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" ></uses-permission>
标签:
原文地址:http://www.cnblogs.com/Miami/p/4226914.html