如何验证WIFI网络密钥?

时间:2019-02-18 11:59:50

标签: android networking wifi android-wifi

我附近有可用的WIFI列表。我想连接特定的wifi。如何使用我选择的特定WIFI进行身份验证。

选择列出的wifi之一并输入相同的密码后,如何验证输入的密码与主机设置的密码相同。主机将如何比较数据?

List of WIFI available near me

1 个答案:

答案 0 :(得分:2)

您需要为此创建一个广播寄存器,如果该寄存器检测到wifi状态发生任何变化,就会触发广播意图。

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);

创建一个本地广播,收听此更新。 例如

 BroadcastReceiver WifiChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (action != null && action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
            NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            Log.d(AppConstants.TAG, "onReceive: \"wifi connected:\" " + networkInfo.isConnected());
            if (networkInfo.isConnected()) {
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo.getSSID().contains("your desired SSID")/*("Android")*/) {
                    isAllowed = true;
                    Log.d(AppConstants.TAG, "==============================onReceive: " + isAllowed);

                    Log.d(AppConstants.TAG, "onReceive:connected to wifi " + wifiInfo.getSSID());
                } else {
                    Log.d(AppConstants.TAG, "onReceive:connected to wifi " + wifiInfo.getSSID());
                    isAllowed = false;
                }

            }
        }
    }
};

如果您已连接wifi,则表示您已通过正确的凭据 否则是错误的