简体   繁体   English

Wifi连接应用程序崩溃(Java,Android)

[英]Wifi-Connecting App Crash (Java, Android)

I'm a complete noob at Android and Java , I've worked with C++ and a little C#. 我是AndroidJava完全菜鸟 ,我使用过C ++和一点C#。 I'm trying to create a simple Android app that connects to a Wifi Network. 我正在尝试创建一个连接到Wifi网络的简单Android应用程序。 The problem is that it crashes on the addNetwork() function. 问题是它在addNetwork()函数上崩溃了

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). 我正在使用目标为2.3.3版Android的项目(我的手机在2​​.3.6上)开发Eclipse。 Do I have to include other files except the import in here (MainActivity.java)? 我必须在此处包含除导入之外的其他文件(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 . 我的附加问题的答案( 我的网络列表附加了相应的SSID,但当我确定它是101%时,它是'不在范围内',因为那是我的路由器!你知道可能出了什么问题吗? )和这里的wifi连接的完整方法。

Well did you declare the permission to use ACCESS_WIFI_STATE In the AndroidMainfest.xml which is in the root of your Android Project. 您是否声明了使用ACCESS_WIFI_STATE的权限在Android项目根目录中的AndroidMainfest.xml中。

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: 对于WEP网络,您需要这样做:

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: 对于WPA网络,您需要添加如下所示的密码:

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

For Open network you need to do this: 对于Open network,您需要这样做:

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

Then, you need to add it to Android wifi manager settings: 然后,您需要将其添加到Android wifi管理器设置:

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): 如果您需要,可以添加此项以启用wifi本身(如果已经存在,状态将不会更改):

wifiManager.setWifiEnabled(true);

And finally, you might need to enable it, so Android conntects to it: 最后,您可能需要启用它,因此Android会对其进行处理:

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. UPD:如果是WEP,如果您的密码是十六进制,则不需要用引号括起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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