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; } } }); } } 
3
  • 5
    If you're having a crash. post the full stack trace from logcat Commented Sep 24, 2018 at 12:31
  • your intent you provided is wrong,WifiConfiguration.this is required you have given WifiConfiguration,this Commented Sep 24, 2018 at 12:31
  • @Basiljose what is the right way ???? because i tried a lot and this is the only way that works for me Commented Sep 24, 2018 at 14:59

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

6 Comments

do u have links to provide me with more details i think this exactly what i need
Let me update the answer with minimal pseudo-code.
ok take your time !
@user10355814, I have updated the answer, have a look at it.
thanks bro ,, but still cant go to the next activity but i think i understood the concept
|
0

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

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

Comments

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); 

Comments

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.