android 连接到 wifi WifiManager.SUPPLICANT_STATE_CHANGED_ACTION 奇怪的问题

android connecting to wifi WifiManager.SUPPLICANT_STATE_CHANGED_ACTION strange issue

我在尝试连接无线网络时遇到问题。 我的代码工作正常,我成功连接到网络,但出现了下述问题。

我有一个包含 wifi 扫描结果的列表视图。

当我第一次点击时,我的接收器没有进入 "completed" 状态。 第二次点击后,没有选择任何网络,它就连接上了,我在 "complete" 中的代码被执行了。

下面的代码是从另一个 class 这就是为什么它是静态的原因

合作代码:

 public static boolean connect(String ssid,String password)
{

    String networkSSID = ssid;
    String networkPass = password;
    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + networkSSID + "\"";
    conf.preSharedKey = "\""+ networkPass +"\"";
    //WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    int netid=mainWifi.addNetwork(conf);
    mainWifi.disconnect();
    mainWifi.enableNetwork(netid, true);

    //mainWifi.reconnect(); <-- exact the same issue discommenting this line
 return true;
}

在 class 连接被调用时,我注册了 BradcastReceiver 如下:

 public void onClick(View v)
        {

            mainWifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            wifiinfo = mainWifi.getConnectionInfo();
            AuthWifi authWifi = new AuthWifi();
            IntentFilter mIntentFilter = new IntentFilter();
            mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
            getApplicationContext().registerReceiver(authWifi, mIntentFilter);
            ClientManager.scan(CameraActivity.this, mainWifi);
        }

我的广播接收器

public class AuthWifi extends BroadcastReceiver {
    @Override
    public void onReceive(Context c, Intent intent) {
        String action = intent.getAction();
        if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {

            SupplicantState supl_state = ((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE));
            switch (supl_state) {

                case COMPLETED:
                  /////////////IF I AM CONNECTED THE WIFI SSID I CHOSE FROM A LISTVIEW THEN ---->
                    if(wifiinfo != null & wifiinfo.getSSID()!=null & ClientManager.getSSID() !=null& !conectado ) {

                        if (wifiinfo.getSSID().contains(ClientManager.getSSID().trim())) ///I know here is better .equals() /// I have contain for my own reasons
                            conectado = true;

/*HERE I DO SOME THINGS WHEN CONNECTED (I CALL A RUNNABLE TO MAKE A SERVER SOCKET)*/


                        }
                    }
                    break;
                case DISCONNECTED:
                    Log.i("SupplicantState", "Disconnected");

                    conectado = false;
                    if (ClientStartReceive.isStopped)
                    {
                        ClientStartReceive.stop();
                    }
                    break;

                default:
                    Log.i("SupplicantState", "Unknown");
                    break;

            }
            int supl_error = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1);
            if (supl_error == WifiManager.ERROR_AUTHENTICATING) {

 /////HERE I MANAGE AUTHENTICATION ERROR

            }
        }
    }
}

希望有人能够提供帮助 :( 如果您需要更多代码来进行故障排除,请告诉我。 如果您有一些参考可以帮助我,即使我需要重建代码也被接受。我的目标是能够连接到网络,显示身份验证错误并在连接成功时执行一些代码。 对不起我的英语,我想你已经猜到我不是本地人了。 问候

如文档所示,“已完成”使用如下:

This state indicates that the supplicant has completed its processing for the association phase and that data connection is fully configured. Note, however, that there may not be any IP address associated with the connection yet.

您不应依赖此状态来确保您的连接已完成。相反,您可以为网络状态更改注册 BroadcastReceiver 侦听器。