1

I'm a complete noob at Android and Java, I've worked with C++ and a little C#. I'm trying to create a simple Android app that connects to a Wifi Network. The problem is that it crashes on the addNetwork() function.

import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

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

        final WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        final WifiConfiguration wc = new WifiConfiguration();        
        String networkSSID = "********";
        String networkPass = "*******";      
        wc.SSID = "\"" + networkSSID + "\""; 
        wc.preSharedKey = "\""+ networkPass +"\"";
        wc.hiddenSSID = true;
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
        wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        int res = wifi.addNetwork(wc);
        wifi.enableNetwork(res, true);
        wifi.setWifiEnabled(true);
        ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 

    }
    }

I'm working on Eclipse with a project with target 2.3.3 version of Android (my phone is on 2.3.6). Do I have to include other files except the import in here (MainActivity.java)?

Edit: Answer to this question found in this this answer. An answer to my additional question ( My list of networks was appended with the corresponding SSID, but was 'not in range' when i'm 101% sure it is, because that is my router! Do you know what could have gone wrong? ) and full How-to for the wifi connection here.

Community
  • 1
  • 1
Bonnev
  • 947
  • 2
  • 9
  • 29

2 Answers2

2

Well did you declare the permission to use ACCESS_WIFI_STATE In the AndroidMainfest.xml which is in the root of your Android Project.

Just open this and put it above or below the application tag

as such

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Mushtaq Jameel
  • 7,053
  • 7
  • 33
  • 52
  • I don't know anything about the Manifest and I can't find the code for it in the project. – Bonnev Jul 05 '13 at 18:00
  • There you go I edited the answer to explain it in more detail. – Mushtaq Jameel Jul 05 '13 at 18:05
  • Thanks! I found it and put the line right above the tag but the result it the same, the app crashes during its opening – Bonnev Jul 05 '13 at 18:10
  • can you paste your Application log(Log-cat) here, that would help. To open log-cat in Eclipse : - Window -- > Show View -- > Other -- > and type in Logcat to filter and select the Logcat which is not deprecated – Mushtaq Jameel Jul 05 '13 at 18:14
  • 1
    Neither user 10082 nor current process has android.permission.CHANGE_WIFI_STATE. I just saw this line, added permissions for it and worked. Although! My list of networks was appended with the corresponding SSID, but was 'not in range' when i'm 101% sure it is, because that is my router! Do you know what could have gone wrong? – Bonnev Jul 05 '13 at 18:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32972/discussion-between-bonnev-and-mushtaq) – Bonnev Jul 05 '13 at 19:41
1

Please refer to this answer for more information, I got this extracted from here

for WEP network you need to do this:

ws.wepKeys[0] = "\"" + networkPass + "\""; 
ws.wepTxKeyIndex = 0;
ws.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
ws.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

For WPA network you need to add passphrase like this:

ws.preSharedKey = "\""+ networkPass +"\"";

For Open network you need to do this:

ws.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

Then, you need to add it to Android wifi manager settings:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.addNetwork(conf);

If you need you can add this to enable the wifi itself (if it already is, the status will not change):

wifiManager.setWifiEnabled(true);

And finally, you might need to enable it, so Android conntects to it:

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

         break;
    }           
 }

UPD: In case of WEP, if your password is in hex, you do not need to surround it with quotes.

Community
  • 1
  • 1
Mushtaq Jameel
  • 7,053
  • 7
  • 33
  • 52
  • Got an instant connection as I ran the program. Thank you for your time and help. – Bonnev Jul 05 '13 at 19:20
  • You are welcome. Please put your final comment explaining the second issue in the Question under and Edit flag so for future reference people will not have any issues with the right answer. – Mushtaq Jameel Jul 05 '13 at 19:28