如何在 Android 9 (Xamarin.Android) 上以编程方式验证用户 WiFi 密码

问题描述

我正在为拥有较旧 Android 型号(特别是 Android 9)的用户添加应用支持,并意识到 Android 明确禁止应用验证用户的 WiFi 密码。我在这里缺少什么?以下评论基于 QA 工程师让我测试的“新 :)”Android 9 Motorola。

我的逻辑:

  1. 如果用户手动移除 WiFi 端点,系统会将其保持 24 小时有效,并且在时间过去之前无法以编程方式重新连接(因此我们无法提示他们移除或断开端点 - 用户必须选择Android 9 中的“忘记” - 因此我们可以在几分钟后在工作流程中连接到它
  2. 如果程序在用户手动连接到另一个 WiFi 端点后尝试连接到另一个 WiFi 端点,系统会立即重新连接到用户选择的手动端点。此外,Android 广播接收器不一致地捕获网络更改流量:WifiManager.SupplicantStateChangedAction 和 WifiManager.SupplicantConnectionChangeAction 完全不稳定且不一致。

最终结果是,Android 强行删除了 3rd 方应用程序以前用于以编程方式验证用户 WiFi 端点密码的所有途径。我知道高层正在努力将安全放在首位;然而,他们为此牺牲了客户效用。

是否有其他途径或方法以编程方式验证用户的 WiFi 密码?至少在更高的操作系统版本(例如 Android 10 +)中,Suggestions 和 Specifier API 会直接询问用户

是否还有其他选项可以以编程方式为用户验证 WiFi 密码?可以想象,对于试图支持物联网平台的人们来说,这是一场噩梦。

TaskResponse ConnectToWiFiBeforeAndroid10(string _ssid,string _password)
{
    bool IsConnected = false;
    ivariable.message = "";

    if (!wifiManager.IsWifiEnabled)
        wifiManager.SetWifiEnabled(true); //turn on wifi if not on

    var formattedSsid = $"\"{_ssid}\"";
    var formattedPassword = $"\"{_password}\"";

    var wifiConfig = new WifiConfiguration
    {
        Ssid = formattedSsid,PreSharedKey = formattedPassword,Priority = 10000,};

    var _networkId = wifiManager.AddNetwork(wifiConfig);
    System.Diagnostics.Debug.WriteLine("addNetwork = " + _networkId);

    //Get the list of configured networks
    var wifiList = wifiManager.Configurednetworks;

    foreach (var i in wifiList)
    {
        if (i.Ssid != null && i.Ssid.Equals("\"" + _ssid + "\""))
        {
            try
            {
                wifiManager.disconnect();
                bool enableNetwork = wifiManager.EnableNetwork(i.NetworkId,true);
                wifiManager.Reconnect();

                Thread.Sleep(10000);

                //Ommitted to save space - check to see if the endpoint that the user wanted to connect to is the one that we connected to    
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("ERROR:",e.Message);
                ivariable.message = e.Message;
            }
        }
    }
}

无法持续工作的广播接收器:

        public override void OnReceive(Context context,Intent intent)
    {
        string action = intent.Action;

        //if (action.Equals(WifiManager.SupplicantConnectionChangeAction))
        if (action.Equals(WifiManager.SupplicantStateChangedAction) || action.Equals(WifiManager.SupplicantConnectionChangeAction))
        {
            //    System.Diagnostics.Debug.WriteLine("WifiReceiver",">>>>SUPPLICANT_STATE_CHANGED_ACTION<<<<<<");
            System.Diagnostics.Debug.WriteLine("WifiReceiver",">>>>NETWORK_STATE_CHANGED_ACTION<<<<<<");
            SupplicantState supl_state = ((SupplicantState)intent.GetParcelableExtra(WifiManager.ExtraNewState));
            SupplicantState supl_conn = ((SupplicantState)intent.GetParcelableExtra(WifiManager.ExtraSupplicantConnected));

            else if (supl_state.Equals(SupplicantState.Completed))
            {
                string connectedSSID = wifiConnector.GetSSID().Replace("\"","");
                System.Diagnostics.Debug.WriteLine("SupplicantState - Connected to " + connectedSSID);
                var val = ivariable.currentSSID;
                if (connectedSSID.Trim().Equals(ivariable.currentSSID))
                {
                    ivariable.DidConnect = 1;
                    ivariable.GlobalSSID = true;
                    System.Diagnostics.Debug.WriteLine("INFO: Connection attempt to " + ivariable.currentSSID + ": TRUE");
                    ivariable.message = $"The system was successfully able to connect to the following network: { ivariable.currentSSID}";
                }
            }

            else if (supl_state.Equals(SupplicantState.Invalid))
            {
                ivariable.DidConnect = 0;
                ivariable.message = "The WiFi name you entered was invalid. Please check the spelling of the name and re-enter again.";
                System.Diagnostics.Debug.WriteLine("SupplicantState","INVALID");
            }
            else if (supl_state.Equals(SupplicantState.Scanning))
            {
                System.Diagnostics.Debug.WriteLine("SupplicantState - SCANNING");
            }
            else if (supl_state.Equals(SupplicantState.Uninitialized))
            {
                System.Diagnostics.Debug.WriteLine("SupplicantState - UNINITIALIZED");
            }
            else
            {
                ivariable.DidConnect = 0;
                System.Diagnostics.Debug.WriteLine("SupplicantState - UnkNown");
            }
            int supl_error = intent.GetIntExtra(WifiManager.ExtraSupplicantError,-1);
            if (supl_error == WifiManager.ErrorAuthenticating)
            {
                ivariable.DidConnect = 0;
                ivariable.message = "The WiFi password you entered was incorrect. Please try again.";
                System.Diagnostics.Debug.WriteLine("SupplicantState - ERROR_AUTHENTICATING!");
            }

            if ((supl_conn != null) && (supl_conn.Equals(SupplicantState.Completed)))
            {
                string connectedSSID = wifiConnector.GetSSID().Replace("\"","");
                System.Diagnostics.Debug.WriteLine("Supplicant Connection - Connected to " + connectedSSID);
                var val = ivariable.currentSSID;
                if (connectedSSID.Trim().Equals(ivariable.currentSSID))
                {
                    ivariable.DidConnect = 1;
                    ivariable.GlobalSSID = true;
                    System.Diagnostics.Debug.WriteLine("INFO: Connection attempt to " + ivariable.currentSSID + ": TRUE");
                    ivariable.message = $"The system was successfully able to connect to the following network: { ivariable.currentSSID}";
                }
            }
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)