Example #1
0
  /**
   * Update the network: either create a new network or modify an existing network
   *
   * @param config the new network configuration
   * @param disableOthers true if other networks must be disabled
   * @return network ID of the connected network.
   */
  private int updateNetwork(WifiConfiguration config, boolean disableOthers) {
    WifiConfiguration found = findNetworkInExistingConfig(config.SSID);
    wifiManager.disconnect();
    if (found == null) {
      statusView.setText(R.string.wifi_creating_network);
    } else {
      statusView.setText(R.string.wifi_modifying_network);
      Log.d(TAG, "Removing network " + found.networkId);
      wifiManager.removeNetwork(found.networkId);
      wifiManager.saveConfiguration();
    }
    networkId = wifiManager.addNetwork(config);
    Log.d(TAG, "Inserted/Modified network " + networkId);
    if (networkId < 0) {
      return FAILURE_NO_NETWORK_ID;
    }

    // Try to disable the current network and start a new one.
    if (!wifiManager.enableNetwork(networkId, disableOthers)) {
      networkId = FAILURE_NO_NETWORK_ID;
      return FAILURE_NO_NETWORK_ID;
    }
    errorCount = 0;
    wifiManager.reassociate();
    return networkId;
  }
Example #2
0
  /**
   * 提供一个外部接口,传入要连接的无线网
   *
   * @param SSID
   * @param Password
   * @param Type
   * @return
   */
  public boolean connect(String SSID, String Password, WifiCipherType Type) {
    if (!this.openWifi()) {
      return false;
    }
    // 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
    // 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
    while (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
      try {
        // 为了避免程序一直while循环,让它睡个100毫秒在检测……
        Thread.currentThread();
        Thread.sleep(100);
      } catch (InterruptedException ie) {
      }
    }

    WifiConfiguration wifiConfig = this.createWifiInfo(SSID, Password, Type);
    //
    if (wifiConfig == null) {
      return false;
    }
    WifiConfiguration tempConfig = this.isExsits(SSID);
    if (tempConfig != null) {
      mWifiManager.removeNetwork(tempConfig.networkId);
    }
    int netID = mWifiManager.addNetwork(wifiConfig);
    // 是否去连接wifi
    boolean isConnect = mWifiManager.enableNetwork(netID, false);
    return isConnect;
  }
Example #3
0
  public WifiConfiguration createWifiInfo(String SSID, String Password, int Type) {
    // Log.v(TAG, "SSID = " + SSID + "## Password = "******"## Type = " + Type);
    System.out.println("SSID = " + SSID + "## Password = "******"## Type = " + Type);
    if (SSID == null || Password == null || SSID.equals("")) {
      // Log.e(TAG, "addNetwork() ## nullpointer error!");
      System.out.println("addNetwork() ## nullpointer error!");
      return null;
    }
    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + SSID + "\"";

    WifiConfiguration tempConfig = isExsits(SSID, mWifiManager);
    if (tempConfig != null) {
      mWifiManager.removeNetwork(tempConfig.networkId);
    }

    if (Type == 1) // WIFICIPHER_NOPASS
    {
      config.wepKeys[0] = "";
      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
      config.wepTxKeyIndex = 0;
    }
    if (Type == 2) // WIFICIPHER_WEP
    {
      config.hiddenSSID = true;
      config.wepKeys[0] = "\"" + Password + "\"";
      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 == 3) // 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.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
      config.status = WifiConfiguration.Status.ENABLED;
    }
    return config;
  }
Example #4
0
 private int doError(int resource_string) {
   statusView.setText(resource_string);
   // Give up on the connection
   wifiManager.disconnect();
   if (networkId > 0) {
     wifiManager.removeNetwork(networkId);
     networkId = -1;
   }
   if (receiverRegistered) {
     unregisterReceiver(wifiReceiver);
     receiverRegistered = false;
   }
   return -1;
 }
Example #5
0
  public WifiConfiguration CreateWifiInfo(String SSID, String Password, int Type) {
    WifiConfiguration config = new WifiConfiguration();
    config.allowedAuthAlgorithms.clear();
    config.allowedGroupCiphers.clear();
    config.allowedKeyManagement.clear();
    config.allowedPairwiseCiphers.clear();
    config.allowedProtocols.clear();
    config.SSID = "\"" + SSID + "\"";

    WifiConfiguration tempConfig = this.IsExsits(SSID);
    if (tempConfig != null) {
      mWifiManager.removeNetwork(tempConfig.networkId);
    }

    if (Type == 1) // WIFICIPHER_NOPASS
    {
      config.wepKeys[0] = "";
      config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
      config.wepTxKeyIndex = 0;
    }
    if (Type == 2) // WIFICIPHER_WEP
    {
      config.hiddenSSID = true;
      config.wepKeys[0] = "\"" + Password + "\"";
      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 == 3) // 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.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
      config.status = WifiConfiguration.Status.ENABLED;
    }
    return config;
  }
Example #6
0
	/**
	 * Function: 提供一个外部接口,传入要连接的无线网 <br>
	 * 
	 * @author ZYT DateTime 2014-5-13 下午11:46:54<br>
	 * @param SSID
	 *            SSID
	 * @param Password
	 * @param Type
	 * <br>
	 *            没密码:{@linkplain WifiCipherType#WIFICIPHER_NOPASS}<br>
	 *            WEP加密: {@linkplain WifiCipherType#WIFICIPHER_WEP}<br>
	 *            WPA加密: {@linkplain WifiCipherType#WIFICIPHER_WPA}
	 * @return true:连接成功;false:连接失败<br>
	 */
	public boolean connect(String SSID, String Password, WifiCipherType Type) {
		if (!this.openWifi()) {
			return false;
		}
		// 开启wifi功能需要一段时间(我在手机上测试一般需要1-3秒左右),所以要等到wifi
		// 状态变成WIFI_STATE_ENABLED的时候才能执行下面的语句
		while (mWifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING) {
			try {
				// 为了避免程序一直while循环,让它睡个100毫秒在检测……
				Thread.currentThread();
				Thread.sleep(100);
			} catch (InterruptedException ie) {
			}
		}

		System.out.println("WifiAdmin#connect==连接结束");

		WifiConfiguration wifiConfig = createWifiInfo(SSID, Password, Type);
		//
		if (wifiConfig == null) {
			return false;
		}

		WifiConfiguration tempConfig = this.isExsits(SSID);

		int tempId = wifiConfig.networkId;
		if (tempConfig != null) {
			tempId = tempConfig.networkId;
			mWifiManager.removeNetwork(tempConfig.networkId);
		}

		int netID = mWifiManager.addNetwork(wifiConfig);

		// 断开连接
		mWifiManager.disconnect();
		// 重新连接
		// netID = wifiConfig.networkId;
		// 设置为true,使其他的连接断开
		boolean bRet = mWifiManager.enableNetwork(netID, true);
		mWifiManager.reconnect();
		return bRet;
	}
 /**
  * Update the network: either create a new network or modify an existing network
  *
  * @param config the new network configuration
  * @return network ID of the connected network.
  */
 private static void updateNetwork(WifiManager wifiManager, WifiConfiguration config) {
   Integer foundNetworkID = findNetworkInExistingConfig(wifiManager, config.SSID);
   if (foundNetworkID != null) {
     Log.i(TAG, "Removing old configuration for network " + config.SSID);
     wifiManager.removeNetwork(foundNetworkID);
     wifiManager.saveConfiguration();
   }
   int networkId = wifiManager.addNetwork(config);
   if (networkId >= 0) {
     // Try to disable the current network and start a new one.
     if (wifiManager.enableNetwork(networkId, true)) {
       Log.i(TAG, "Associating to network " + config.SSID);
       wifiManager.saveConfiguration();
     } else {
       Log.w(TAG, "Failed to enable network " + config.SSID);
     }
   } else {
     Log.w(TAG, "Unable to add network " + config.SSID);
   }
 }
Example #8
0
  /**
   * Ensure no more than numOpenNetworksKept open networks in configuration list.
   *
   * @param wifiMgr
   * @param numOpenNetworksKept
   * @return Operation succeed or not.
   */
  private boolean checkForExcessOpenNetworkAndSave(int numOpenNetworksKept) {
    final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks();
    sortByPriority(configurations);
    boolean modified = false;
    int tempCount = 0;
    for (int i = configurations.size() - 1; i >= 0; i--) {
      final WifiConfiguration config = configurations.get(i);
      if (ConfigSec.isOpenNetwork(ConfigSec.getWifiConfigurationSecurity(config))) {
        tempCount++;
        if (tempCount >= numOpenNetworksKept) {
          modified = true;
          mWifiManager.removeNetwork(config.networkId);
        }
      }
    }
    if (modified) {
      return mWifiManager.saveConfiguration();
    }

    return true;
  }
Example #9
0
 /**
  * 删除一个 路由。测试路由不需要保持,直接删除即可
  *
  * @param netId
  */
 public void deleWifi(int netId) {
   mWifiManager.removeNetwork(netId);
 }
Example #10
0
  public WifiConfiguration createWifiInfo(String ssid, String passawrd, int paramInt, String type) {
    // ?????????????
    WifiConfiguration localWifiConfiguration1 = new WifiConfiguration();
    // ????????????????
    localWifiConfiguration1.allowedAuthAlgorithms.clear();
    localWifiConfiguration1.allowedGroupCiphers.clear();
    localWifiConfiguration1.allowedKeyManagement.clear();
    localWifiConfiguration1.allowedPairwiseCiphers.clear();
    localWifiConfiguration1.allowedProtocols.clear();

    if (type.equals("wt")) { // wifi????
      localWifiConfiguration1.SSID = ("\"" + ssid + "\"");
      WifiConfiguration localWifiConfiguration2 = isExsits(ssid);
      if (localWifiConfiguration2 != null) {
        mWifiManager.removeNetwork(
            localWifiConfiguration2.networkId); // ???б???????????????????????
      }
      if (paramInt == 1) { // ???????
        localWifiConfiguration1.wepKeys[0] = "";
        localWifiConfiguration1.allowedKeyManagement.set(0);
        localWifiConfiguration1.wepTxKeyIndex = 0;
      } else if (paramInt == 2) { // ??????
        localWifiConfiguration1.hiddenSSID = true;
        localWifiConfiguration1.wepKeys[0] = ("\"" + passawrd + "\"");
      } else { // wap????
        localWifiConfiguration1.preSharedKey = ("\"" + passawrd + "\"");
        localWifiConfiguration1.hiddenSSID = true;
        localWifiConfiguration1.allowedAuthAlgorithms.set(0);
        localWifiConfiguration1.allowedGroupCiphers.set(2);
        localWifiConfiguration1.allowedKeyManagement.set(1);
        localWifiConfiguration1.allowedPairwiseCiphers.set(1);
        localWifiConfiguration1.allowedGroupCiphers.set(3);
        localWifiConfiguration1.allowedPairwiseCiphers.set(2);
      }
    } else { // "ap" wifi???
      localWifiConfiguration1.SSID = ssid;
      localWifiConfiguration1.allowedAuthAlgorithms.set(1);
      localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
      localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
      localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
      localWifiConfiguration1.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
      localWifiConfiguration1.allowedKeyManagement.set(0);
      localWifiConfiguration1.wepTxKeyIndex = 0;
      if (paramInt == 1) { // ???????
        localWifiConfiguration1.wepKeys[0] = "";
        localWifiConfiguration1.allowedKeyManagement.set(0);
        localWifiConfiguration1.wepTxKeyIndex = 0;
      } else if (paramInt == 2) { // ??????
        localWifiConfiguration1.hiddenSSID = true; // ?????????ssid
        localWifiConfiguration1.wepKeys[0] = passawrd;
      } else if (paramInt == 3) { // wap????
        localWifiConfiguration1.preSharedKey = passawrd;
        localWifiConfiguration1.allowedAuthAlgorithms.set(0);
        localWifiConfiguration1.allowedProtocols.set(1);
        localWifiConfiguration1.allowedProtocols.set(0);
        localWifiConfiguration1.allowedKeyManagement.set(1);
        localWifiConfiguration1.allowedPairwiseCiphers.set(2);
        localWifiConfiguration1.allowedPairwiseCiphers.set(1);
      }
    }
    return localWifiConfiguration1;
  }