Ejemplo n.º 1
0
  /**
   * This method initiates the connect process
   *
   * @param config an array containing the SSID and the password of the network
   * @param context android context
   */
  public void connect(String[] config, Context context) {

    identificationMissing = false;
    wlanKeyMissing = false;

    wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    adhoc = new AdhocWifiManager(wifi);
    ssid = config[0];

    identification = preferences.getString("identification", "");

    if (identification.equals("")) {
      identificationMissing = true;
    }

    boolean connectedSuccessful = false;

    // check whether the network is already known, i.e. the password is
    // already stored in the device
    for (WifiConfiguration configuredNetwork : wifi.getConfiguredNetworks()) {
      if (configuredNetwork.SSID.equals("\"".concat(ssid).concat("\""))) {
        connectedSuccessful = true;
        networkId = configuredNetwork.networkId;
        break;
      }
    }

    if (!connectedSuccessful) {
      for (ScanResult result : wifi.getScanResults()) {
        if (result.SSID.equals(ssid)) {
          connectedSuccessful = true;

          if (result.capabilities.contains("WPA") || result.capabilities.contains("WEP")) {
            wlanKeyMissing = true;
          }
          break;
        }
      }
    }

    if (connectedSuccessful) {

      if (identificationMissing || wlanKeyMissing) {
        identificationWlanKeyDialogFragment =
            new IdentificationWlanKeyDialogFragment(identificationMissing, wlanKeyMissing);
        identificationWlanKeyDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.Dialog);
        identificationWlanKeyDialogFragment.show(
            getFragmentManager(), "identificationWlanKeyDialogFragment");
      } else {
        adhoc.connectToNetwork(networkId, this);
      }
    } else {
      for (int i = 0; i < 2; i++)
        Toast.makeText(
                this, getString(R.string.toast_network_not_found_text, ssid), Toast.LENGTH_SHORT)
            .show();
    }
  }
Ejemplo n.º 2
0
  @Override
  public void onDialogPositiveClick(DialogFragment dialog) {
    if (identificationMissing) {
      SharedPreferences.Editor editor = preferences.edit();
      editor.putString(
          "identification", ((IdentificationWlanKeyDialogFragment) dialog).getIdentification());
      editor.commit();
    }

    if (wlanKeyMissing) {
      adhoc.connectToNetwork(
          ssid, ((IdentificationWlanKeyDialogFragment) dialog).getWlanKey(), this);
    } else {
      adhoc.connectToNetwork(networkId, this);
    }

    dialog.dismiss();
  }