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