3

While developing an app where I scan the WiFi, I found that it does not work if I turn off the location service on my phone. I have provided the app with all the necessary permissions. - ACCESS_NETWORK_STATE, ACCESS_WIFI_STATE, ACCESS_COARSE_LOCATION.

This is my code:

WifiManager manager= (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
manager.startScan();

In the receiver:

int found = manager.getScanResults().size();

This question answers part of the problem. Wifi scan results broadcast receiver not working

My questions are:

  1. Is there a way for the app to list the Wifi access points if the location service is turned off?

  2. If location service is absolutely necessary, is there a way for the app to turn on the location service while the app scans the wifi access points?

sudhir shakya
  • 827
  • 1
  • 9
  • 21
  • 1
    Google thinks that using Bluetooth or Wifi you can get the user's location while he doesn't want you to do that. That's why starting from Android 6 you have to request location permission to scan/connect to WIFI to clearly state that you are probably able to get the user's location using that services. Also AFAIK you don't have to turn on location services to scan WIFI, if you request location permissions. – Vladyslav Matviienko Nov 22 '17 at 08:24
  • @VladMatvienko When I turned off the location service, the count of Wifi access point was 0, when I turned it on, the count was the actual number of Wifi points. I am using Android 6.0 – sudhir shakya Nov 22 '17 at 08:35
  • 1
    Have you requested the location permissions before getting WIFI networks? – Vladyslav Matviienko Nov 22 '17 at 08:40
  • Yes. I have. Seems to be an Android bug. https://stackoverflow.com/questions/35361064/wifi-getscanresults-returns-an-empty-list-if-gps-is-off-android-6-0-1?rq=1 – sudhir shakya Nov 22 '17 at 09:32
  • Are you sure that you have requested it in runtime, not only declared it in the manifest? – Vladyslav Matviienko Nov 22 '17 at 10:37
  • Yes I have. Without requesting it in runtime, it would not work (with or without the Location Service). – sudhir shakya Nov 22 '17 at 10:43

3 Answers3

3

The only way to get the scanResult without GPS turned on is to set the app's targetSDKversion to 21 or lower.

This will work even on Lolipop and above.

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
lakshman.pasala
  • 565
  • 6
  • 16
1

Android 8.0 and Android 8.1:

A successful call to WifiManager.getScanResults() requires any one of the following permissions:

  • ACCESS_FINE_LOCATION
  • ACCESS_COARSE_LOCATION
  • CHANGE_WIFI_STATE

If the calling app does not have any of these permissions, the call fails with a SecurityException.

Android 9 and later:

A successful call to WifiManager.startScan() requires all of the following conditions to be met:

  • Your app has the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission.
  • Your app has the CHANGE_WIFI_STATE permission.
  • Location services are enabled on the device (under Settings > Location).

To successfully call WifiManager.getScanResults() ensure all of the following conditions are met:

  • Your app has the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission.
  • Your app has the ACCESS_WIFI_STATE permission.
  • Location services are enabled on the device (under Settings > Location).

If the calling app doesn't meet all of these requirements, the call fails with a SecurityException.


This is from https://developer.android.com/guide/topics/connectivity/wifi-scan Google Documentation. Probably they needed the "Location services are enabled on the device" requirement for Android 6.+ because it is the version this permission restrictions first revealed, but they don't seem to had this requirement in documentations since no one has answered this question until now.

Kevin
  • 2,258
  • 1
  • 32
  • 40
0

Is there a way for the app to list the Wifi access points if the location service is turned off?

Yes, only system apps can get scan results without the location with the following permission:

android.permission.PEERS_MAC_ADDRESS permission

Amirhosein
  • 4,266
  • 4
  • 22
  • 35