1

I don't know why my application crashes when order the next activity although its working fine in other applications.

Basically I want the user to connect to specific wifi (that I have mentioned by WIFIConfiguration)

If connected he should go to the main activity otherwise he cant access the other activity. What is wrong and is there any better way to authenticate if the user is connected to wifi?

wifi.java class

public class WiFiConfiguration extends Activity {

Button btnnext;

public String networkSSID = null;
public String networkPass = null;

public Button ConnectButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_wifi_configuration);

    btnnext = (Button) findViewById(R.id.btnnext);
    ConnectButton = (Button)findViewById(R.id.connButton);


    /*btnnext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(WiFiConfiguration,this secondActivity.class);

            startActivity(i);
        }
    });*/



    ConnectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            networkSSID = "myssid";
            networkPass = "mypass";

            WifiConfiguration conf = new WifiConfiguration();
            conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain ssid in quotes

            /* for wep*/
            //conf.wepKeys[0] = "\"" + networkPass + "\"";
            //conf.wepTxKeyIndex = 0;
            //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            //conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);

            /* for wpa*/
            conf.preSharedKey = "\""+ networkPass +"\"";

            /* for open network*/
            //conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

            Context context = getApplicationContext();
            WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

            wifiManager.setWifiEnabled(true);

            wifiManager.addNetwork(conf);

            List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
            for( WifiConfiguration i : list )
                if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
                    wifiManager.disconnect();
                    wifiManager.enableNetwork(i.networkId, true);
                    wifiManager.reconnect();

                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    while (wifiInfo.getSSID() == null) {
                        Log.i("WifiStatus", "Here I am");
                        try {
                            Thread.sleep(Time.SECOND);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        wifiInfo = wifiManager.getConnectionInfo();
                    }

                    System.out.println("Connection established");
                    Toast.makeText(context, "Connection established", 1000).show();


                    Intent i = new Intent(WiFiConfiguration,this secondActivity.class);

                    startActivity(i);


                    break;


                }
        }
    });



}

}
Michael Dodd
  • 10,102
  • 12
  • 51
  • 64

4 Answers4

0

Do it like this.. You have added ',' before this

                    Intent i = new Intent(WiFiConfiguration.this,  secondActivity.class);
                    startActivity(i);
Ali Ahmed
  • 2,130
  • 1
  • 13
  • 19
0

You have a very simple typo in your listener.

Instead of

Intent i = new Intent(WiFiConfiguration,this secondActivity.class);

You should write WiFiConfiguration.this to reference the current Context and correctly place the comma after that.

Intent i = new Intent(WiFiConfiguration.this, secondActivity.class);
startActivity(i);
Markus Penguin
  • 1,581
  • 12
  • 19
0

I think WifiManager#reconnect() is an asynchronous call. Therefore, we should register a broadcast receiver with WifiManager#WIFI_STATE_CHANGED_ACTION action. Please refer to the below snippet:

  private BroadcastReceiver mWifiStateChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action)) {
                if (action.equalsIgnoreCase(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
                    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                    if (connectivityManager != null) {
                        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                        if (networkInfo != null) {
                            // just to be more sure, you can also check whether the connected APN as same as the required one
                            // by comparing APN names
                            if (networkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
                                Intent i = new Intent(context, secondActivity.class);                        
                                startActivity(i);
                            }
                        }
                    }
                }
            }
        }
    }
};

Make sure to register and unregister the receiver like this:

 @Override
protected void onStart() {
    super.onStart();
    IntentFilter intentFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
    this.registerReceiver(mWifiStateChangeReceiver, intentFilter);
}

@Override
protected void onStop() {
    super.onStop();
    this.unregisterReceiver(mWifiStateChangeReceiver);
}

When we catch this action, we should use ConnectivityManager.getActiveNetworkInfo() to get NetworkInfo which again has inner-enum NetworkInfo.State#CONNECTED.

We can use this enum to check if WifiManager#reconnect() call is completed and we're connected to an APN, and after this check we can start new activity here.

Jay
  • 2,852
  • 1
  • 15
  • 28
0

You have added "," instead of "." while passing context in intent

You need to write

 Intent i = new Intent(WiFiConfiguration.this secondActivity.class);
 startActivity(i);

instead of

  Intent i = new Intent(WiFiConfiguration,this secondActivity.class);
  startActivity(i);