Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Android
Dear All,
I need your help. I use Android to connect to an unsecure wireless network with the name specified, such as "TEST";
I use blow code for test, but it fails. So could any one help me?
public class WifiConnActivity extends Activity {
    /** Called when the activity is first created. */
	final String tag = "WifiConn:...";
	EditText txt;
	WifiManager mWifi;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        mWifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
 
        txt = (EditText)findViewById(R.id.editText1);
 
        Button b1 = (Button)findViewById(R.id.B1);        
        b1.setOnClickListener(new OnClickListener(){
            @Override
        	public void onClick(View v)
        	{
                
                if (mWifi.startScan())
                {
                	Log.d(tag, "startScan()");
 
                	List<scanresult> sRet = mWifi.getScanResults();
 
                	for (int i=0; i<sret.size();>                	{
                    	ScanResult retS = sRet.get(i); 
                    	txt.append("resT: " + retS.SSID +" " + retS.BSSID + "\n");
                    	Log.d(tag, "resT: " + retS.SSID +" " + retS.BSSID);
                    	
                    	if (retS.SSID.equalsIgnoreCase("TEST")) // TEST is wireless SSID
                    	{
                        	txt.append("Found: " + retS.SSID +" " + retS.BSSID + "\n");
                   		
                    		WifiConfiguration wc = new WifiConfiguration();
 
                    		wc.SSID = "\""+retS.SSID+"\"";
                    		wc.BSSID = retS.BSSID;
                    		wc.status = WifiConfiguration.Status.ENABLED;
                    		wc.hiddenSSID = true;
                    		
                    		int netID = mWifi.addNetwork(wc); // add network
                    		txt.append("addNetwork: "+ Integer.toString(netID) +"\n");
                    		
                    		if(mWifi.enableNetwork(netID, true)) // enable network
                    		{
                    			txt.append("enableNetwork: true\n");
                    		}
                    	}
                	}
                	
                }
        	}
        	
        });      
    }    
}
Posted 19-Aug-11 16:35pm
Edited 19-Aug-11 17:08pm
thatraja241.9K
v2
Comments
DaveAuld at 20-Aug-11 11:40am
   
Are there any exceptions thrown, do any of the methods return anything like timeout, or unable to connect etc. etc. Do you have any exception handling that may trap the errors?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Below is the answer.
     wc.wepKeys[0] = "";    
     wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);    
     wc.wepTxKeyIndex = 0; 
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 15 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100