public class WifiAutoConnectManager {
private static final String TAG = WifiAutoConnectManager.class.getSimpleName);
WifiManager wifiManager;
// 定义几种加密方式,一种是WEP,一种是WPA,还有没有密码的情况
public enum WifiCipherType {
WIFICIPHER_WEP, WIFICIPHER_WPA, WIFICIPHER_NOPASS, WIFICIPHER_INVALID
}
// 构造函数
public WifiAutoConnectManagerWifiManager wifiManager) {
this.wifiManager = wifiManager;
}
// 提供一个外部接口,传入要连接的无线网
public void connectString ssid, String password, WifiCipherType type) {
Thread thread = new Threadnew ConnectRunnablessid, password, type));
thread.start);
}
// 查看以前是否也配置过这个网络
private WifiConfiguration isExsitsString SSID) {
List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks);
for WifiConfiguration existingConfig : existingConfigs) {
if existingConfig.SSID.equals””” + SSID + “””)) {
return existingConfig;
}
}
return null;
}
private WifiConfiguration createWifiInfoString 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 + “””;
// nopass
if Type == WifiCipherType.WIFICIPHER_NOPASS) {
config.wepKeys[0] = “”;
config.allowedKeyManagement.setWifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
// wep
if Type == WifiCipherType.WIFICIPHER_WEP) {
if !TextUtils.isEmptyPassword)) {
if isHexWepKeyPassword)) {
config.wepKeys[0] = Password;
} else {
config.wepKeys[0] = “”” + Password + “””;
}
}
config.allowedAuthAlgorithms.setAuthAlgorithm.OPEN);
config.allowedAuthAlgorithms.setAuthAlgorithm.SHARED);
config.allowedKeyManagement.setKeyMgmt.NONE);
config.wepTxKeyIndex = 0;
}
// wpa
if Type == WifiCipherType.WIFICIPHER_WPA) {
config.preSharedKey = “”” + Password + “””;
config.hiddenSSID = true;
config.allowedAuthAlgorithms.setWifiConfiguration.AuthAlgorithm.OPEN);
config.allowedGroupCiphers.setWifiConfiguration.GroupCipher.TKIP);
config.allowedKeyManagement.setWifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.setWifiConfiguration.PairwiseCipher.TKIP);
// 此处需要修改否则不能自动重联
// config.allowedProtocols.setWifiConfiguration.Protocol.WPA);
config.allowedGroupCiphers.setWifiConfiguration.GroupCipher.CCMP);
config.allowedPairwiseCiphers.setWifiConfiguration.PairwiseCipher.CCMP);
config.status = WifiConfiguration.Status.ENABLED;
}
return config;
}
// 打开wifi功能
private boolean openWifi) {
boolean bRet = true;
if !wifiManager.isWifiEnabled)) {
bRet = wifiManager.setWifiEnabledtrue);
}
return bRet;
}
class ConnectRunnable implements Runnable {
private String ssid;
private String password;
private WifiCipherType type;
public ConnectRunnableString ssid, String password, WifiCipherType type) {
this.ssid = ssid;
this.password = password;
this.type = type;
}
@Override
public void run) {
// 打开wifi
openWifi);
// 开启wifi功能需要一段时间我在手机上测试一般需要1-3秒左右),所以要等到wifi
// 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
while wifiManager.getWifiState) == WifiManager.WIFI_STATE_ENABLING) {
try {
// 为了避免程序一直while循环,让它睡个100毫秒检测……
Thread.sleep100);
} catch InterruptedException ie) {
}
}
WifiConfiguration wifiConfig = createWifiInfossid, password, type);
//
if wifiConfig == null) {
Log.dTAG, “wifiConfig is null!”);
return;
}
WifiConfiguration tempConfig = isExsitsssid);
if tempConfig != null) {
wifiManager.removeNetworktempConfig.networkId);
}
int netID = wifiManager.addNetworkwifiConfig);
boolean enabled = wifiManager.enableNetworknetID, true);
Log.dTAG, “enableNetwork status enable=” + enabled);
boolean connected = wifiManager.reconnect);
Log.dTAG, “enableNetwork connected=” + connected);
}
}
private static boolean isHexWepKeyString wepKey) {
final int len = wepKey.length);
// WEP-40, WEP-104, and some vendors using 256-bit WEP WEP-232?)
if len != 10 && len != 26 && len != 58) {
return false;
}
return isHexwepKey);
}
private static boolean isHexString key) {
for int i = key.length) – 1; i >= 0; i–) {
final char c = key.charAti);
if !c >= ‘0’ && c <= ‘9’ || c >= ‘A’ && c <= ‘F’ || c >= ‘a’ && c <= ‘f’)) {
return false;
}
}
return true;
}
}