无法连接到Wi-Fi网络 [英] Can't connect to WiFi network

查看:324
本文介绍了无法连接到Wi-Fi网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新android开发,并尝试使用Android SDK来连接到Wi-Fi网络。在$ C $下断开工作正常,但重新连接失败。这里的code,我有

I am new to android development and was trying to connect to WiFi network using the Android SDK. The code for disconnect works fine but re-connection fails. Here's the code that i have

try {
        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";   // Please note the quotes. String should contain SSID in quotes
        conf.wepKeys[0] = password;  //WEP password is in hex, we do not need to surround it with quotes.
        conf.wepTxKeyIndex = 0;
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

        WifiManager wifiManager = (WifiManager)ba.applicationContext.getSystemService(Context.WIFI_SERVICE);
        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();               

                 break;
            }           
         }

        //WiFi Connection success, return true
        return true;
    } catch (Exception ex) {

        throw ex;
    }

我包装这个code中,我使用在不同的应用程序中的jar文件。当我把这种方法,并尝试使用该SSID和密码连接到WEP的网络,我不断收到以下错误:

I am wrapping this code in a jar file which I am using in different application. When I call this method and try to connect to WEP network using the SSID and password, I keep on getting the following error:

android.system.ErrNoException:recvfrom的失败:ETIMEDOUT(连接超时)

android.system.ErrNoException: recvfrom failed: ETIMEDOUT (Connection timed out).

该错误不会告诉,有一个连接超时的地方,但我一直没能想出解决办法来解决我的code。任何指针和变化,我可以介绍给code,使这项工作?

The error does tell that there is a connection timeout somewhere, but I haven't been able to figure this out to fix my code. Any pointers and changes that I can introduce to code to make this work??

Paritosh

推荐答案

连接信息可以异步到达,所以你可以不知道在code你提到阉连接成功与否。你可以尝试实现一个BroadcastReceiver,是可以获得wifi连接的信息。

Connection information can arrive asynchronously, so you cannot know in the code you mentioned wether the connection was successful or not. You can try to implement a BroadcastReceiver, that gets the information of the wifi connection.

public class ConnectivityChangedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] netInf = conMgr.getAllNetworkInfo();

        for (NetworkInfo inf : netInf) {
            if (inf.getTypeName().contains("wifi")) {
                if (inf.isConnected()) {
                    Toast.makeText(context, "Connected to Wifi", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context, "Could not connect to wifi", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}

然后,在你的Andr​​oid清单,你应该将它声明为beeing一个接收器,像这样的:

Then, in your android manifest you should declare it as beeing a receiver, like this:

<receiver android:name=".YourPackageName.ConnectivityChangedReceiver" >
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        <action android:name="android.net.wifi.STATE_CHANGE" />
        </intent-filter>
</receiver>

我只是想这对我自己,但我认为这是正确的解决此问题,因为Wifimanager.reconnect()并没有真正连接我配置的网络。祝您好运。

I am just trying this on my own now, but I think that this is the correct workaround for this issue, as Wifimanager.reconnect() didn't really connected me to the configured network. Best of luck.

这篇关于无法连接到Wi-Fi网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆