Beispiel #1
0
 private int shiftPriorityAndSave() {
   final List<WifiConfiguration> configurations = mWifiManager.getConfiguredNetworks();
   sortByPriority(configurations);
   final int size = configurations.size();
   for (int i = 0; i < size; i++) {
     final WifiConfiguration config = configurations.get(i);
     config.priority = i;
     mWifiManager.updateNetwork(config);
   }
   mWifiManager.saveConfiguration();
   return size;
 }
Beispiel #2
0
 /**
  * Change the password of an existing configured network and connect to it
  *
  * @param wifiMgr
  * @param config
  * @param newPassword
  * @return
  */
 public boolean changePasswordAndConnect(
     WifiConfiguration config, String newPassword, int numOpenNetworksKept) {
   ConfigSec.setupSecurity(config, ConfigSec.getWifiConfigurationSecurity(config), newPassword);
   final int networkId = mWifiManager.updateNetwork(config);
   if (networkId == -1) {
     // Update failed.
     return false;
   }
   // Force the change to apply.
   mWifiManager.disconnect();
   return connectToConfiguredNetwork(config, true);
 }
 /**
  * Wifiネットワークに接続する
  *
  * @return boolean
  */
 public boolean connect(String ssid) {
   int networkId = 0;
   WifiConfiguration config = getWifiConfig(ssid);
   networkId = mWifiManager.addNetwork(config);
   if (networkId == -1) {
     return false;
   }
   mWifiManager.saveConfiguration();
   mWifiManager.updateNetwork(config);
   config.networkId = networkId;
   return connect(config);
 }
Beispiel #4
0
  /**
   * Connect to a configured network.
   *
   * @param wifiManager
   * @param config
   * @param numOpenNetworksKept Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT
   * @return
   */
  public boolean connectToConfiguredNetwork(WifiConfiguration config, boolean reassociate) {
    final String security = ConfigSec.getWifiConfigurationSecurity(config);
    int oldPri = config.priority;
    // Make it the highest priority.
    int newPri = getMaxPriority() + 1;
    if (newPri > MAX_PRIORITY) {
      newPri = shiftPriorityAndSave();
      config = getWifiConfiguration(config, security);
      if (config == null) {
        return false;
      }
    }

    // Set highest priority to this configured network
    config.priority = newPri;
    int networkId = mWifiManager.updateNetwork(config);
    if (networkId == -1) {
      return false;
    }

    // Do not disable others
    if (!mWifiManager.enableNetwork(networkId, false)) {
      config.priority = oldPri;
      return false;
    }

    if (!mWifiManager.saveConfiguration()) {
      config.priority = oldPri;
      return false;
    }

    // We have to retrieve the WifiConfiguration after save.
    config = getWifiConfiguration(config, security);
    if (config == null) {
      return false;
    }

    // ReenableAllApsWhenNetworkStateChanged.schedule(mContext);

    // Disable others, but do not save.
    // Just to force the WifiManager to connect to it.
    if (!mWifiManager.enableNetwork(config.networkId, true)) {
      return false;
    }

    final boolean connect = reassociate ? mWifiManager.reassociate() : mWifiManager.reconnect();
    if (!connect) {
      return false;
    }

    return true;
  }
 /**
  * Wifiネットワークに接続する
  *
  * @return boolean
  */
 public boolean connect(String ssid, String password) {
   int networkId = 0;
   Security security = getWifiSecurity(ssid);
   WifiConfiguration config = createWifiConfig(security, ssid, password);
   networkId = mWifiManager.addNetwork(config);
   if (networkId == -1) {
     return false;
   }
   mWifiManager.saveConfiguration();
   mWifiManager.updateNetwork(config);
   config.networkId = networkId;
   return connect(config);
 }
Beispiel #6
0
  // 链接并且保持wifi
  public boolean ConnectASaveWifi(int wifiId) {
    mWifiConfiguration = mWifiManager.getConfiguredNetworks(); // ??????ú?????????

    for (int i = 0; i < mWifiConfiguration.size(); i++) {
      WifiConfiguration wifi = mWifiConfiguration.get(i);
      if (wifi.networkId == wifiId) {
        while (!(mWifiManager.enableNetwork(wifiId, true))) {
          mWifiManager.updateNetwork(wifi);
          Log.i("ConnectWifi", String.valueOf(mWifiConfiguration.get(wifiId).status));
        }

        return true;
      }
    }
    return false;
  }
  /**
   * Enable the scan access point which has no security. This may result in the asynchronous
   * delivery of state change events. Applications should register the
   * 'SUPPLICANT_STATE_CHANGED_ACTION' and 'NETWORK_STATE_CHANGED_ACTION' receiver to receive the
   * state change events.
   *
   * @param One of the access point which want to link.
   * @return Return true if the operation succeed, false otherwise.
   */
  public boolean enableNetworkLink(ScanResult result) {
    if (WifiConst.DummyMode) {
      return false;
    }

    int networkId = -1;
    if (result == null) {
      return false;
    }

    if (isConfigured(result)) {
      if (localLOGV)
        NetLog.d(
            TAG, "[WifiAvailableAp][EnableNetworkLink]: " + result.SSID + " has been configured.");
      networkId = getNetworkId(result);
    } else {
      if (localLOGV)
        NetLog.d(
            TAG,
            "[WifiAvailableAp][EnableNetworkLink]: " + result.SSID + " has not been configured.");
      WifiConfiguration config = new WifiConfiguration();

      config.SSID = WifiUtil.convertToQuotedString(result.SSID);
      config.allowedKeyManagement.set(KeyMgmt.NONE);
      config.status = WifiConfiguration.Status.ENABLED;
      networkId = mWifiManager.addNetwork(config);
      mWifiManager.updateNetwork(config);
    }

    if (networkId == -1) {
      return false;
    }

    if (mWifiManager.enableNetwork(networkId, true)) {
      mWifiManager.reconnect();

      if (localLOGV)
        NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: enable network link success.");
      return true;
    }

    return false;
  }
  /**
   * Enable the scan access point which has security. This may result in the asynchronous delivery
   * of state change events. Applications should register the 'SUPPLICANT_STATE_CHANGED_ACTION' and
   * 'NETWORK_STATE_CHANGED_ACTION' receiver to receive the state change events.
   *
   * @param One of the access point which want to link.
   * @return Return true if the operation succeed, false otherwise.
   */
  public boolean enableNetworkLink(ScanResult result, String sectretKey) {
    if (WifiConst.DummyMode) {
      return false;
    }

    int networkId = -1;
    int keyLength = sectretKey.length();
    WifiConfiguration config = new WifiConfiguration();
    if (localLOGV)
      NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: Enable Access Point with Password.");
    if (result == null || sectretKey == null) {
      return false;
    }

    WifiUtil.clearConfiguration(config);
    config.status = WifiConfiguration.Status.ENABLED;

    config.SSID = WifiUtil.convertToQuotedString(result.SSID);

    int encrypt = WifiUtil.getEncrypt(result);

    if (encrypt == WifiConst.W_ENCRYPT_WEP) {
      if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: WEP encrypt.");
      if ((keyLength == 5 || keyLength == 10 || keyLength == 13 || keyLength == 26)
          && sectretKey.matches("[0-9A-Fa-f]*")) {
        config.wepKeys[0] = sectretKey;
      } else {
        config.wepKeys[0] = '"' + sectretKey + '"';
      }
    } else {
      if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: other encrypt.");
      if (sectretKey.matches("[0-9A-Fa-f]{64}")) {
        config.preSharedKey = sectretKey;
      } else {
        config.preSharedKey = "\"" + sectretKey + "\"";
      }
    }

    int ConfirmType = WifiUtil.getConfirmType(result);
    switch (ConfirmType) {
      case WifiConst.W_CONFIRM_OPEN:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_OPEN");
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        config.allowedAuthAlgorithms.clear();
        config.allowedPairwiseCiphers.set(PairwiseCipher.NONE);
        config.allowedGroupCiphers.clear();
        config.allowedProtocols.clear();
        break;

      case WifiConst.W_CONFIRM_WEP:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_WEP");
        config.allowedKeyManagement.set(KeyMgmt.NONE);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
        config.allowedPairwiseCiphers.set(PairwiseCipher.NONE);

        if (keyLength == 5 || keyLength == 10) {
          config.allowedGroupCiphers.set(GroupCipher.WEP40);
        } else {
          config.allowedGroupCiphers.set(GroupCipher.WEP104);
        }

        config.allowedProtocols.clear();
        break;

      case WifiConst.W_CONFIRM_WPA_PSK:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_WPA_PSK");
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedProtocols.set(Protocol.WPA);
        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
        break;

      case WifiConst.W_CONFIRM_WPA2_PSK:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_WPA2_PSK");
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedProtocols.set(Protocol.RSN);
        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
        break;

      case WifiConst.W_CONFIRM_PSK_AUTO:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_PSK_AUTO");
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedProtocols.set(Protocol.RSN);
        config.allowedProtocols.set(Protocol.WPA);
        config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
        break;

      case WifiConst.W_CONFIRM_WPA_EAP:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_WPA_EAP");
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedProtocols.set(Protocol.WPA);
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        break;

      case WifiConst.W_CONFIRM_WPA2_EAP:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_WPA2_EAP");
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedProtocols.set(Protocol.RSN);
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        break;

      case WifiConst.W_CONFIRM_EAP_AUTO:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_CONFIRM_EAP_AUTO");
        config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
        config.allowedProtocols.set(Protocol.RSN);
        config.allowedProtocols.set(Protocol.WPA);
        config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
        break;

      default:
        break;
    }

    switch (encrypt) {
      case WifiConst.W_ENCRYPT_WEP:
        break;

      case WifiConst.W_ENCRYPT_TKIP:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_ENCRYPT_TKIP");
        config.allowedGroupCiphers.set(GroupCipher.TKIP);
        config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
        break;

      case WifiConst.W_ENCRYPT_AES:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_ENCRYPT_AES");
        config.allowedGroupCiphers.set(GroupCipher.CCMP);
        config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
        break;

      case WifiConst.W_ENCRYPT_TKIP_AES:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_ENCRYPT_TKIP_AES");
        config.allowedGroupCiphers.set(GroupCipher.TKIP);
        config.allowedGroupCiphers.set(GroupCipher.CCMP);
        config.allowedPairwiseCiphers.set(PairwiseCipher.TKIP);
        config.allowedPairwiseCiphers.set(PairwiseCipher.CCMP);
        break;

      default:
        if (localLOGV) NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: W_ENCRYPT default.");
        break;
    }

    networkId = mWifiManager.addNetwork(config);
    if (localLOGV)
      NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: 398  networkId -> " + networkId);
    mWifiManager.updateNetwork(config);

    if (networkId == -1) {
      return false;
    }

    if (mWifiManager.enableNetwork(networkId, true)) {
      if (localLOGV)
        NetLog.d(TAG, "[WifiAvailableAp][EnableNetworkLink]: Enable network link SUCCESS.");

      mWifiManager.reconnect();

      return true;
    }

    return false;
  }