在Android 5和6

时间:2017-01-18 06:03:56

标签: android android-5.0-lollipop android-6.0-marshmallow android-wifi wifimanager

在Android 5和6中连接到WiFi网络已经有一段时间了,而其他类似的问题似乎对我不起作用。我可以在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();

这就是我注意到的。

  

mWifiManager.addNetwork(CONF)

使用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? 

App permissions

1 个答案:

答案 0 :(得分:0)

我相信如果你添加一个disconnect()它会做你想要的。我在此处添加了一项检查,因为addNetwork()无法添加您的网络。对于Android 6.0+,如果网络已经存在,则无法添加网络,但如果此网络失败,您可以尝试获取网络ID,然后进行连接。 (例如,如果重新安装,它会在那里添加(并保存))我也不会在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();