【发布时间】:2017-01-18 06:03:56
【问题描述】:
在 Android 5 和 6 中连接 WiFi 网络有一段时间了,其他类似的问题似乎对我不起作用。我可以在 Android 4.4.2 中获得相同的代码
在下面添加代码sn-p。
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();
这就是我注意到的。
mWifiManager.addNetwork(conf)
返回 Android 5(phone) 和 Android 6(tab) 的一些 (+) 整数。
但两者都无法连接,除非我打开 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?
【问题讨论】:
-
在尝试连接到特定 SSID 时,您是否已经连接到移动数据或任何其他 WiFI SSID?您可以尝试使用
ConnectivityManager添加NetworkCallback并查看是否为您尝试连接的SSID 获得onNetworkAvailable()。我还建议注册NETWORK_STATE_CHANGED_ACTION和WIFI_STATE_CHANGED_ACTION广播,并在尝试连接期间查看不同的网络和 wifi 状态。 -
我添加了侦听器来检测状态变化,但我没有收到任何回调。根据我的观察添加了一些修改。
-
有什么特别的理由从服务中获取 WifiManager 的实例吗?您是在
Service和Activities之间共享实例,还是只有Service与WifiManager交互?此外,所有这些方法都应该为您提供不同状态的状态更改回调。我认为这不是多个引用的问题,而是事件同步的问题。enableNetwork()本身会尝试连接到提供的 networkId,因此您不需要立即执行它(您不需要 reconnect() 来尝试连接) -
service中只有与WifiManager交互的实例。我确实通过addNetwork和enableNetwork电话获得了肯定的价值观。但是接入点的最终状态是WIFI_AP_DISABLED。从 onCreate 或从活动中进行相同的调用似乎可以使其正常工作。
标签: android android-5.0-lollipop android-6.0-marshmallow android-wifi wifimanager