当前位置: 首页 > 知识库问答 >
问题:

在Android 5和6中自动连接到WiFi网络

商弘义
2023-03-14

一直在努力连接到WiFi网络在Android 5和6一段时间和其他类似的问题似乎不适合我。我可以得到相同的代码工作在Android 4.4.2

添加下面的代码片段。

        String networkSSID = getSsid();
        String networkPass = getNetworkPass();

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + networkSSID + "\"";
        conf.status = WifiConfiguration.Status.ENABLED;
        conf.priority = 40;

        conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

        conf.preSharedKey = "\""+ networkPass +"\"";
        int value = mWifiManager.addNetwork(conf);
        Log.i(TAG_CHECK, "Connecting to : " + value);
        boolean enabled = mWifiManager.enableNetwork(value, true);
        Log.i(TAG_CHECK, "enabled to : " + enabled);
        mWifiManager.reconnect();

这是我注意到的。

我是经理。添加网络(配置)

返回一些()整数与Android 5(手机)和Android 6(标签).

但是,除非我打开wifi设置(我不需要做任何事情,只要在那里登陆即可连接),或者我从顶部栏手动关闭并打开wifi以自动连接,否则两者都不会连接。

用于检测网络连接的侦听器能够准确地知道两个版本何时建立连接,从而确认上述行为。已附上下面授予的权限的图片。

有没有关于我遗漏了什么的建议?

EDIT:深入到WifiManager类后,似乎接入点仍处于WIFI_AP_STATE_DISABLED状态。我还应该强调,在尝试不同的Android M手机时,一切都按预期进行。

EDIT2

I have the following observations.

1. The issue so far is specific to 1 android 6.0.1 Samsung tablet and 1 android 5.0.2 Micromax phone. It works just fine on 3 other android 6.0.1 phones, 1 android N phone and Android 4.4.2 phone. 
2. The access point ends up in wifi_ap_disabled state in the problematic cases consistently. Both addNetwork and enableNetwork calls are affirmative.
3. These access points are not that of a router wifi but that of other phones that broadcast. The problematic phones can programatically connect to wifi hotspots (setup manually and not in the programatic way as I would like to) without any issue.
4. Mobile data enabled/disabled state or wifi state with a different connected network doesn't change the dynamics for both working and problematic phones. 

This makes me think that it is a combination of phones/tabs (and not OS) and the access point broadcast configuration. Do you think I should be playing around with some config parameters?



Edit 3 - Important Update

So the wifimanager is obtained like below

WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);

I instantiate mWifiManager in a service (inside onCreate method) and use that later to scan and connect with inputs from front-end activity. While it doesn't work in the way as described earlier, using the same snippet seems to work when the enableNetwork call is done right in the onCreate method - working just as expected. It also works fine when called from front-end activity but only when I don't instantiate mWifiManager in the service.

Though I would expect only one reference to a system service - WifiManager i.e, I think the subsequent usage of that reference (or a different reference to WifiManager) gets a lower priority (or gets deprioritized  by the previous reference)  and hence doesn't get completely executed leaving the access point in disabled state and requiring manual intervention to help complete the execution by WifiManager and other wifi system services.

Also, I notice the same in Android 5.0.2 phone, the asynchronous enableNetwork does get executed, but it takes some time to execute it.

My questions
1. It is no more a question about the correctness of the code. Services in general have lesser priority compared to front-end threads So, is there is way I could prioritise the enableNetwork call so that it gets immediately executed?
2. What happens in case of multiple references to WifiManager? 

共有1个答案

祁博雅
2023-03-14

我相信如果你添加一个dislink(),它会做你想要的。我在这里添加了一个检查,因为addNetwork()可能无法添加您的网络。对于Android6.0,如果网络已经存在,你不能添加网络,但是如果失败,你可以尝试获取网络标识然后连接。(例如,如果你重新安装,它会在那里添加(并保存))我也不会在Lollipop(第21版)中向SSID添加引号。

int value = mWifiManager.addNetwork(conf);
// need to check the result if successful
if (value == -1) {
    return; // network not added successfully
}
Log.i(TAG_CHECK, "Connecting to : " + value);
mWifiManager.disconnect();  // add a disconnect
boolean enabled = mWifiManager.enableNetwork(value, true);
Log.i(TAG_CHECK, "enabled to : " + enabled);
mWifiManager.reconnect();
 类似资料:
  • Android将WIFI详细信息保存到WifiConfiguration类中。当WIFI为ON和SSID时,Pass-key详细信息与WIFI网络匹配,然后Android自动连接特定的WIFI网络。如何禁用该自动连接功能?问这个问题的目的是,我有两个配置的网络(已经保存),我想在按钮单击事件上连接WIFI,但android的自动连接功能与WIFI本身连接。

  • 设置wifi账号和密码,连接特定的wifi网络。 请求方式: "|2|1|wifi_ssid,wifi_pwd|\r" 参数: wifi_ssid wifi账号的SSID wifi_pwd wifi账号密码 返回值: "|2|1|\r" wifi连接状态:wifi断开连接 "|2|2|\r" wifi连接状态:正在连接wifi "|2|3|ip|\r" wifi连接状态:wifi连接成功,返回OB

  • 目的尝试连接或切换到特定的WiFi网络,无需任何人为干预(用户名和密码除外)。 代码片段 主要活动。kt 类主活动:AppCompat活动(){ } AndroidManifest。xml权限 使用此代码,设备能够连接到WiFi网络,但需要手动关闭和打开WiFi。必须有更好的方法来连接或切换到特定的WiFi网络,而无需任何手动操作。 当前流程: 运行应用程序并单击主屏幕上的按钮连接到WiFi。 转

  • 我正在尝试在这里制作一个应用程序,它将检测WiFi网络中连接的所有设备。我已经做了足够多的谷歌,并提出了一个应用程序,可以检测在应用程序的WiFi网络中连接的设备的IP地址。 现在我想要更多的东西。 < li >我能否找到设备名称,即电话名称或型号或系统名称,以及我们可以用来检测特定设备的任何信息? < li >我们能否找到设备距离,例如该设备距离我们使用应用的手机有多远? < li >这是主要任

  • 问题内容: 我拥有连接到特定访问点的所有详细信息。我只需要使用该访问点,因此我所需要的只是执行此操作的命令。 问题答案: 您可以自己(或在脚本中)将网络条目添加到wpa_supplicant.conf中,基本上手动进行一次连接,然后执行以下操作: 并将网络条目集成到脚本中以实现自动化。示例简单脚本:

  • 我实际上是在编程一些东西,不让用户访问Android原生wifi连接页面,所以我在我的Android应用程序中编程。它扫描可用的无线网络,将其显示在列表视图中,然后单击要连接的网络。 事实上,除了我的连接功能之外,一切都很好,这使得我的应用程序崩溃,所以如果你有任何想法...下面是代码(这是用户输入网络通行证后调用的函数)和堆栈跟踪: 堆栈跟踪: