简体   繁体   中英

Wifi-Connecting App Crash (Java, Android)

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 .

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" />

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM