WIFI自动打开并连接到指定的热点:
do { try { //连接到指定的wifi热点 mAdmin.connectWifi(SSID, PWD, WifiCipherType.WIFICIPHER_WPA); Thread.sleep(20 * 1000); } catch (InterruptedException e) { // TODO Auto-generated catch block Log.i(TAG, "connect wifi failed. please open wifi !"); break; } } while (!mAdmin.isWifiConnect()); //判断wifi是否连接成功连接指定wifi热点
public void connectWifi(String SSID, String Password, WifiCipherType Type) { //打开wifi if (!this.openWifi()) { return; } //等到wifi状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句 while (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) { try { // 为了避免程序一直while循环,让它睡个100毫秒在检测…… Thread.currentThread(); Thread.sleep(100); } catch (InterruptedException ie) { } } //判断是否已配置过当前热点 WifiConfiguration config = IsConfig(SSID); if (config == null) { //创建一个当前连接的wifi热点 config = CreateWifiInfo(SSID, Password, Type); }else { mWifiManager.removeNetwork(config.networkId); } WifiManager.Channel mChannel = mWifiManager.initialize(mContext, mContext.getMainLooper(), null); mWifiManager.connect(mChannel, config, null); }判断是否已经配置过当前wifi热点
public WifiConfiguration IsConfig(String SSID) { List<WifiConfiguration> existingConfigs = mWifiManager.getConfiguredNetworks(); if (existingConfigs != null && existingConfigs.size() > 0) { for (WifiConfiguration existingConfig : existingConfigs) { if (existingConfig.SSID.equals("\"" + SSID + "\"")) { return existingConfig; } } } return null; }添加一个wifi热点信息配置
public WifiConfiguration CreateWifiInfo(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; } 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; } 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; }打开或关闭wifi连接
// 打开WIFI public boolean openWifi() { boolean bRet = true; if (!mWifiManager.isWifiEnabled()) { bRet = mWifiManager.setWifiEnabled(true); } return bRet; } // 关闭WIFI public void closeWifi() { if (mWifiManager.isWifiEnabled()) { mWifiManager.setWifiEnabled(false); } }判断wifi是否成功连接
public boolean isWifiConnect() { ConnectivityManager mConnectivityManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWifi = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); return mWifi.isConnected(); }