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

在Android 5和6中自动连接到WiFi网络,android,android-5.0-lollipop,android-6.0-marshmallow,android-wifi,wifimanager,Android,Android 5.0 Lollipop,Android 6.0 Marshmallow,Android Wifi,Wifimanager,在安卓5和安卓6上连接WiFi网络已经有一段时间了,其他类似的问题似乎对我不起作用。我可以在Android 4.4.2中使用相同的代码 在下面添加代码段 String networkSSID = getSsid(); String networkPass = getNetworkPass(); WifiConfiguration conf = new WifiConfiguration(); conf.SSID = "\"" +

在安卓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)

使用安卓5(手机)和安卓6(制表符)返回一些(+)整数

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

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

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

编辑
:深入查看WifiManager类后,接入点似乎仍处于WIFI\u AP\u状态\u禁用状态。我还应该强调的是,在不同的安卓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? 

我相信,如果您添加断开连接(),它将实现您想要的功能。我在这里添加了一个复选框,因为addNetwork()可能无法添加您的网络。对于Android 6.0+,如果网络已经存在,则无法添加网络,但如果失败,则可以尝试获取网络id,然后进行连接。(例如,如果您重新安装,它将被添加(并保存)在那里)我也不会在Lollipop(Ver21)+中向SSID添加引号


我确实得到了正确的网络id(已经添加了),但enableNetwork似乎不起作用。但无论我是否明确断开连接,当前网络(与目标网络不同)似乎在任何情况下都断开连接。我在Android SDK中调试WifiManager时遇到问题。我看到其他函数在enableNetwork中执行其他函数。我怀疑这会导致目标网络由于某种原因出现WIFI\u AP\u状态\u禁用状态。当进入wifi设置屏幕时,它会连接。当您尝试连接到特定SSID时,是否已连接到移动数据或任何其他wifi SSID?是否可以尝试使用
ConnectivityManager
添加
NetworkCallback
,并查看是否为您尝试连接的SSID获取
onNetworkAvailable()
。我还建议注册
NETWORK\u STATE\u CHANGED\u ACTION
WIFI\u STATE\u CHANGED\u ACTION
广播,并在尝试连接期间查看网络和WIFI的不同状态。我添加了侦听器以检测状态更改,我没有收到任何回调。在我的观察中添加了一些编辑。从服务中获取WifiManager实例有什么特别的原因吗?您是在
服务
活动
之间共享实例,还是仅与
WifiManager
交互的
服务
?此外,所有这些方法都应该为不同的状态提供状态更改回调。我不认为这是一个多引用的问题,但关于事件的同步
enableNetwork()
本身尝试连接到提供的网络ID,因此您不需要立即执行它(您不需要重新连接()来尝试连接)。
服务中只有与
WifiManager
交互的实例。通过
addNetwork
enableNetwork
调用,我确实得到了肯定的值。但接入点的最终状态为
WIFI\u AP\u DISABLED
。从onCreate或从activity发出相同的调用似乎可以使其正常工作。
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();