protected WifiAccessPoint(WifiConfiguration config) {

    ssid = (config.SSID == null ? "" : WifiUtil.removeDoubleQuotes(config.SSID));
    security = WifiUtil.getSecurity(config);
    networkId = config.networkId;
    mConfig = config;
    mRssi = Integer.MAX_VALUE;
  }
 protected WifiAccessPoint(ScanResult result) {
   ssid = result.SSID;
   security = WifiUtil.getEncrypt(result);
   networkId = -1;
   mRssi = result.level;
   mResult = result;
 }
  private void updateAccessPoints(List<ScanResult> scanResultList) {
    List<WifiAccessPoint> accessPoints = new ArrayList<WifiAccessPoint>();

    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    int ip = wifiInfo.getIpAddress();
    String ssid = wifiInfo.getSSID();

    List<ScanResult> results = scanResultList;
    if (results != null) {
      for (ScanResult result : results) {
        if (result.SSID == null
            || result.SSID.length() == 0
            || result.capabilities.contains("[IBSS]")) {
          continue;
        }
        boolean found = false;
        for (WifiAccessPoint accessPoint : accessPoints) {
          if (accessPoint.update(result)) {
            found = true;
          }
        }
        if (!found) {
          WifiAccessPoint accessPoint = new WifiAccessPoint(result);
          ssid = WifiUtil.removeDoubleQuotes(ssid);

          if (ssid != null && result.SSID.compareTo(ssid) == 0 && ip > 0) {
            accessPoint.setConnectStatus(WifiConst.CONNCETED);
          }
          accessPoints.add(accessPoint);
        }
      }
    }
    mAccessPoints = accessPoints;
  }
 boolean update(ScanResult result) {
   if (ssid.equals(result.SSID) && security == WifiUtil.getEncrypt(result)) {
     if (WifiManager.compareSignalLevel(result.level, mRssi) > 0) {
       mRssi = result.level;
     }
     return true;
   }
   return false;
 }
  private int getNetworkId(ScanResult result) {
    mWifiConfigList = mWifiManager.getConfiguredNetworks();
    int total = mWifiConfigList.size();
    for (int i = 0; i < total; i++) {
      if (mWifiConfigList.get(i).SSID.equals(WifiUtil.convertToQuotedString(result.SSID))) {
        return mWifiConfigList.get(i).networkId;
      }
    }

    return -1;
  }
  private boolean isConfigured(ScanResult result) {
    if (WifiConst.DummyMode) {
      return false;
    }

    mWifiConfigList = mWifiManager.getConfiguredNetworks();

    int total = mWifiConfigList.size();
    for (int i = 0; i < total; i++) {

      if (mWifiConfigList.get(i).SSID.equals(WifiUtil.convertToQuotedString(result.SSID))) {
        return true;
      }
    }
    return false;
  }
  public String getWifiProtocal() {
    int channel = WifiUtil.getChannelNumber(mResult.frequency);
    // 802.11n
    if (channel == 3 || channel == 11) {
      return "N";
    }

    // 802.11g/n
    else if (channel == 1 || channel == 5 || channel == 9 || channel == 13) {
      return "N'G";
    }

    // 802.11b
    //		else if (channel == 1 || channel == 6 || channel == 11 || channel == 14){
    //
    //		}

    return "N'G";
  }
  /**
   * 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;
  }
  public int getProtocol() {

    return WifiUtil.getConfirmType(mResult);
  }
  /**
   * 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;
  }