我如何知道wifi连接有效? [英] How do I know the wifi connection worked?

查看:102
本文介绍了我如何知道wifi连接有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最后我自己找到了一个答案,请在下面查看我的答案

我目前正在开始wifi扫描

I currently start a wifi scan

    wifiReceiver = new WifiReceiver();
    registerReceiver(wifiReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    wifiManager.startScan();

,然后在我的广播接收器中连接到wifi

and in my broadcastreceiver connect to a wifi

class WifiReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("DEBUG", "Wifi scanned");
        SmartWifiChange(); //connect to certain wifi if stuff.
        unregisterReceiver(this);
        stopSelf(); //end of a running service
    }
}

SmartWifiChange()部分:

SmartWifiChange() partial:

        WifiConfiguration wifiToConnect = presentWifis.get(resultToConnect);
        success = wifiManager.enableNetwork(wifiToConnect.networkId, true);
        if(success)
            Toast.makeText(getApplicationContext(), "Now connected to: "
            +resultToConnect.SSID,Toast.LENGTH_LONG).show();
        else presentWifis.remove(resultToConnect);

我实际上是想这样做,然后检查我是否真的连接了,或者再试一次没有。现在,我只是 do {做这个} while(!success),但看来成功永远都是是的,实际上该连接是否有效。

I actually want to do this and then check if I'm actually connected, or try again if it didn't. Right now, I'm just do{ing this }while(!success), but it seems success is always true, wether or not the connection actually worked.

最后,我如何询问或获得有关wifi连接是否有效的广播?

How can I, in the end, ask or get a broadcast about wether or not the wifi connection worked?

编辑:

找到 ConnectivityManager.NetworkCallback 并希望要在这里使用它,我只需要正确理解它即可:

Found the ConnectivityManager.NetworkCallback and would like to use it here, I just need to understand it properly:

最后,我想知道我当前命令的连接是否失败,所以我可以继续。所以我实际上需要一种onConnectionFailed()。如果了解给定的回调,也许我仍然可以构建它。

In the end, I want to know if my currently commanded connection failed so I can move on. So I actually need kind of an onConnectionFailed(). Maybe I could still build that, if understand the given callbacks.

通常,通过OS手动连接到网络时,会获得有关连接...和身份验证失败的状态更新。实际上,这正是我所需要的失败消息。在这些回调上,我基本上得到了onAvailable(),onLost()和onLosing()...如何使用那里获得身份验证失败消息?

Usually, when manually connecting to a network via OS, you get status updated about "connecting..." and "authentification failed". That's actually exactly what I need, that "failed" message. On these Callbacks I essentially got onAvailable(), onLost() and onLosing()...how to use there to get my "authentification failed" message?

编辑2 :

一个小时后,我实际上找到了一个可能的解决方案,我将尝试使用该解决方案,然后报告:

After an hour of googeling this and that, I actually found a possible solution, which I will try to utilize and then report back:

Android中的Wifi验证错误

编辑3:尝试了该解决方案,看起来不错,但由于某些原因无法正常工作。我仍然不太了解这种联系,无法说明原因。我没主意,所以这里是我的代码和LogCat; WLAN-R44是我的,我将其更改为密码错误,因此希望我的手机连接到我的邻居所知道的邻居的 Fritzbox ... bla ... Wifi,但从来没有来在这里试用:

Edit 3: Tried that solution, seems right but doesn't work for some reason. I still don't understand this connecting thing enough to say why. I'm out of ideas, so here's my code and LogCat; WLAN-R44 is mine, I changed it to have a wrong password and so wanted my phone to get connected to the "Fritzbox...bla..." Wifi of my neighbor, which is known to my phone, but never even comes up for trial here:

接收器:

class WifiReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {

        switch(intent.getAction()) {
            case WifiManager.SCAN_RESULTS_AVAILABLE_ACTION:
                Log.e("DEBUG", "Wifi scanned");
                unregisterReceiver(this);
                if(SmartWifiChange(true)){
                    registerReceiver(this, new IntentFilter(WifiManager
                            .SUPPLICANT_STATE_CHANGED_ACTION));
                }else {
                    Log.e("Debug", "Off");
                    stopSelf();
                }
                break;
            case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
                Log.e("Debug", "State changed action");
                Log.e("Debug", "New state: "+intent.getParcelableExtra(WifiManager
                        .EXTRA_NEW_STATE).toString());
                if (intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)==WifiManager.ERROR_AUTHENTICATING)
                    Log.e("Debug", "Error authenticating");
                    if (!SmartWifiChange(false)){
                        unregisterReceiver(this);
                        Log.e("AfterError", "Off");
                        stopSelf();
                    }
                else if ((intent.getParcelableExtra(WifiManager
                        .EXTRA_NEW_STATE))==SupplicantState.COMPLETED){
                        Log.e("Debug", "Completed");
                    Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
                    unregisterReceiver(this);
                        Log.e("Completed", "Off");
                    stopSelf();
                }else{
                        Toast.makeText(context, "Problem", Toast.LENGTH_SHORT).show();
                        Log.e("Problem", intent.getParcelableExtra(WifiManager
                            .EXTRA_NEW_STATE).toString());
                        Log.e("Problem", "Off");
                        unregisterReceiver(this);
                        stopSelf();
                }
                break;
            default:
                stopSelf();

        }
    }
}

连接方法:

private boolean SmartWifiChange(boolean success) {
    boolean connectToWifi = true;
    parseWifis();

        //Keine bekannten Wifis vorhanden
        if (presentWifis.isEmpty()){
            wifiManager.setWifiEnabled(false); //WLAN aus
            Toast.makeText(getApplicationContext(), "No known Wifis -> mobile data",
                    Toast.LENGTH_LONG).show();
            setMobileData(true);//Datenverbindung an
            connectToWifi=false;
        }else{
            if(success) {
                ScanResult[] keysArray = presentWifis.keySet().toArray(new ScanResult[presentWifis
                        .keySet().size()]);
                resultToConnect = keysArray[0];
                for (int i = 1; i < presentWifis.size(); i++) {
                    if (keysArray[i].level > resultToConnect.level) {
                        resultToConnect = keysArray[i];
                    }
                }
                WifiConfiguration wifiToConnect = presentWifis.get(resultToConnect);
                wifiManager.enableNetwork(wifiToConnect.networkId, true);
                Toast.makeText(getApplicationContext(), "Now connecting to: " + resultToConnect
                                .SSID,
                        Toast.LENGTH_SHORT).show();
                Log.e("Debug", resultToConnect.SSID);
            }
            else {
                Log.e("Debug", "Neuer Versuch");
                presentWifis.remove(resultToConnect);
                connectToWifi=SmartWifiChange(true);
            }
            if(connectToWifi)
                setMobileData(false);
        }
    return connectToWifi;
}

LogCat:

04-21 15:39:30.025  16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Service started
04-21 15:39:30.057  16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:32.828  16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Wifi scanned
04-21 15:39:33.105  16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.230  16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ WLAN-R44
04-21 15:39:33.237  16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.244  16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ State changed action
04-21 15:39:33.245  16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ New state: FOUR_WAY_HANDSHAKE
04-21 15:39:33.263  16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ Neuer Versuch
04-21 15:39:33.280  16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.285  16243-16243/de.regenhardt.smartwifiwidget E/Debug﹕ WLAN-R44
04-21 15:39:33.285  16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.286  16243-16243/de.regenhardt.smartwifiwidget E/DEBUG﹕ Data: false
04-21 15:39:33.288  16243-16243/de.regenhardt.smartwifiwidget E/TextView﹕ get resource from application failed.
04-21 15:39:33.291  16243-16243/de.regenhardt.smartwifiwidget E/Problem﹕ FOUR_WAY_HANDSHAKE
04-21 15:39:33.291  16243-16243/de.regenhardt.smartwifiwidget E/Problem﹕ Off


推荐答案

所以,我终于找到了答案,然后再试许多不成功的事情:

So, I finally found an answer, aftertrying many unsuccessful things:

要按预期停止服务,我的onReceive现在几乎只这样做:

To stop the Service as intended, my onReceive pretty much only does this now:

    public void onReceive(Context context, Intent intent) {
        Log.e("DEBUG", "Received");
        try {
            switch(intent.getAction()) {
                case WifiManager.SUPPLICANT_STATE_CHANGED_ACTION:
                    Log.e("Debug", "State changed action");
                    Log.e("Debug", "New state: "+intent.getParcelableExtra(WifiManager
                            .EXTRA_NEW_STATE).toString());
                    if (intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1)==WifiManager.ERROR_AUTHENTICATING) {
                        Log.e("Debug", "Error authenticating");
                        if (!SmartWifiChange(false)) { //retry with next available Wifi
                            unregisterReceiver(this);
                            Log.e("AfterError", "Off");
                            stopSelf();
                        }
                    }
                    else if ((intent.getParcelableExtra(WifiManager
                            .EXTRA_NEW_STATE))==SupplicantState.COMPLETED){
                            Log.e("Debug", "Completed");
                        Toast.makeText(context, "Done", Toast.LENGTH_LONG).show();
                        unregisterReceiver(this);
                            Log.e("Completed", "Off");
                        stopSelf();
                    }
                    break;
                default:
                    stopSelf();

            }
        } catch (Exception e) {
            e.printStackTrace();
            unregisterReceiver(this);
            stopSelf();
        }
    }

现在,我的Smart Wifi Widget实际上效果很好,并且

Now my Smart Wifi Widget actually works great, and as of somebody actually paying for a dev account for me it's for free on the play store, as I made it because there wasn't anything like it but there should be.

这篇关于我如何知道wifi连接有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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