/**
   * 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();
    }
  }
示例#2
0
    @Override
    public void onReceive(Context c, Intent intent) {
      List<ScanResult> results = nm.wifi.getScanResults();
      ScanResult bestSignal = null;

      if (nm.state == State.SCANNING) {
        Log.i("starwisp", "Scanning " + nm.state);

        for (ScanResult result : results) {
          if (result.SSID.equals(SSID)) {
            Log.i("starwisp", "scan result for..." + SSID);
            m_Builder.DialogCallback(m_Context, m_Context.m_Name, m_CallbackName, "\"In range\"");
            nm.Connect();
            return;
          }
        }

        if (nm.state == State.SCANNING) {
          Log.i("starwisp", "REScanning " + nm.state);
          nm.wifi.startScan();
        }
      }
    }
  public void connectToAP(String ssid, String passkey, Context ctx) {
    //	    Log.i(TAG, "* connectToAP");

    WifiConfiguration wifiConfiguration = new WifiConfiguration();
    WifiManager wifiManager = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
    String networkSSID = ssid;
    String networkPass = passkey;

    //	    Log.d(TAG, "# password " + networkPass);
    if (results != null && results.size() > 0) {
      for (ScanResult result : results) {
        if (result.SSID.equals(networkSSID)) {

          String securityMode = getScanResultSecurity(result);

          if (securityMode.equalsIgnoreCase("OPEN")) {

            wifiConfiguration.SSID = "\"" + networkSSID + "\"";
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            int res = wifiManager.addNetwork(wifiConfiguration);
            //	                Log.d(TAG, "# add Network returned " + res);

            wifiManager.enableNetwork(res, true);
            //	                Log.d(TAG, "# enableNetwork returned " + b);

            wifiManager.setWifiEnabled(true);

          } else if (securityMode.equalsIgnoreCase("WEP")) {

            wifiConfiguration.SSID = "\"" + networkSSID + "\"";
            wifiConfiguration.wepKeys[0] = "\"" + networkPass + "\"";
            wifiConfiguration.wepTxKeyIndex = 0;
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            int res = wifiManager.addNetwork(wifiConfiguration);
            //	                Log.d(TAG, "### 1 ### add Network returned " + res);

            wifiManager.enableNetwork(res, true);
            //	                Log.d(TAG, "# enableNetwork returned " + b);

            wifiManager.setWifiEnabled(true);
          }

          wifiConfiguration.SSID = "\"" + networkSSID + "\"";
          wifiConfiguration.preSharedKey = "\"" + networkPass + "\"";
          wifiConfiguration.hiddenSSID = true;
          wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
          wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
          wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
          wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
          wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
          wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
          wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
          wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

          int res = wifiManager.addNetwork(wifiConfiguration);

          wifiManager.enableNetwork(res, true);

          boolean changeHappen = wifiManager.saveConfiguration();

          wifiManager.setWifiEnabled(true);
          if (res != -1 && changeHappen) {

            if (!checkNetwrk())
              Toast.makeText(
                      ctx,
                      "wifi password is set but no internet access. Restart the router",
                      Toast.LENGTH_LONG)
                  .show();
            else Toast.makeText(ctx, "wifi is connected", Toast.LENGTH_LONG).show();
            try {
              Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            Intent i = new Intent(ctx, InternetConnectionSetting.class);
            startActivity(i);
            finish();
          }
        }
      }
    } else {
      getWifiList();
    }
  }