Application (WiFi connections) doesn't work anymore on Android 6.0 Marshmallow

落爺英雄遲暮 提交于 2019-12-04 11:22:27

So I eventually figured it out with some help of the people commenting and reading on the web. So it was a permissions problem after all.

It seems in order to work well with WifiManager when scanning the Connections from Android 6.0 it needs to access your location, so that is either the fine location or the coarse location, I added the following to my Manifest file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I was checking in my code whether the connection was available before connecting it, and that's why it would never work, it always returned 0 results from the network scan because the permission is missing. So in a standard way an implementation needs to be done to request the ACCESS_COARSE_LOCATION permission, afterwards it would not work, UNLESS you turned on your location setting in Android. It actually makes my app pretty useless if I need to turn on location every time...

At the end I did a work around without checking whether the access point is available and just doing a try-catch statement to try to connect to it. It's ugly but it's the only way in new Android.

I do like the new permission model, but apparently Google has done a really bad job implementing it at some points. Why would you need now to turn on the location to be able to get the WiFi scan results???? Doesn't make any sense because it worked before Android 6.0 without that. I do understand that about the location permission, but actually having to turn on location to be able to scan what's available on WiFi is just plain wrong...

An excellent tutorial advice:

*App on Marshmallow needs ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission and Location services should be enabled to get wifi scan results

You can read more about this at: http://www.intentfilter.com/2016/08/programatically-connecting-to-wifi.html

Reading about a restriction that was put starting from Marshmallow was that the app can only modify those WiFi networks that it has created. The system doesn't allow modification of networks that other apps or users have created. So, adding the network might fail if the network already exists in the list (the system might auto connect to network once it's detected).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!