I have LeEco2. I am working on an app and I see something strange.
In my App permissions in Location, I see that location has been disabled for app but in Location->Location Access, it shows Allowed
In App Management, I see Permissions has no access to location
Location->Location Access in the settings is to enable the GPS and the Permissions in your app settings is Permission for your app to use the Location Service.
You can have the following.
You can request for Location Permission and It can be given while the GPS is still turned off.
LOCATION SERVICE could be on and your app could have got the permission but then user can go back and turn off either of them.
I suggest you always check if GPS is ON before triggering location permission and before using it as well.
Related
I'm looking for a simple way to know the GPS permission status on an iOS/Android device.
I'll give an example to clarify my question:
If the GPS on the device is turned off (so there is no way to obtain the longitude/latitude coordinates) I would display a label in this way.
<p *ngIf="!isGPSTurnedOn()">GPS is turned off, please go to settings and turn it on!<\p>
And in the ts file:
isGPSTurnedOn() { return .... }
There is also the possibility that the GPS is turned on but the location permission is not granted by the user. Something like this
<p *ngIf="!isGPSPermissionGranted()">Location permission is not granted. Please go to settings and grant it!<\p>
Same story in the ts file.
My final goal is to try to obtain GPS position if both permissions are granted: otherwise, the user will be warn about the issue.
Thanks in advance!
What you need is the Ionic Native - Diagnostic plugin.
https://ionicframework.com/docs/native/diagnostic/
With this plugin, you can check whether device features are enabled or not, GPS included.
Some useful methods (from plugin documentation):
isLocationEnabled() :
Returns true if the device setting for location is on. On Android this returns true if Location Mode is switched on. On iOS this returns true if Location Services is switched on.
isLocationAuthorized() :
Checks if the application is authorized to use location. Note for Android: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
getLocationAuthorizationStatus() :
Returns the location authorization status for the application.
requestLocationAuthorization(mode) :
Returns the location authorization status for the application. Note for Android: this is intended for Android 6 / API 23 and above. Calling on Android 5 / API 22 and below will always return GRANTED status as permissions are already granted at installation time.
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:
Is there a way for the app to list the Wifi access points if the location service is turned off?
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?
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.
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.
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
I need to find out if location permission for current app is disabled or not.
I can use cordova.plugins.diagnostic.isLocationEnabled to check if global phone's location services are turned off or on, but that doesn't take into account permissions settings for current app.
I tried using navigator.geolocation.getCurrentPosition but it never fails if i disable location permission, it just times out giving me error code 3 which is timeout error. But I need to differentiate between request timeout and location permission being turned off
Ok I found out the answer. This is Android 6 specific issue. It has 2 levels of location settings. Global and app's specific. cordova.plugins.diagnostic will add support for app's specific permission settings soon.
https://github.com/dpa99c/cordova-diagnostic-plugin/issues/11
During resolution, SettingAPI will present dialog, but it contains everything that it detects the user needs to activate. For example, WIFI and GPS. I would like to prompt the user to turn on only GPS for example, but not WIFI for security reasons. If I send them to settings instead of the using Plays ,new Settings API they have the option of turning everything on. I am trying to limit to just one type of setting and I would like to use the SettingAPI dialog for this.
The best bet here is not to add the
android.permission.ACCESS_WIFI_STATE permission.
This way the app won't have any information about Wi-Fi Networks.
But do add the
android.permission.ACCESS_COARSE_LOCATION and android.permission.ACCESS_FINE_LOCATION in the Manifest file so that the app can always look for the GPS.
Hope this Helps!!
SettingsApi doesn't allow you to specify location providers directly (GPS location, network location provider, etc). Instead, you can only ask for location priorities, and SettingsApi chooses location providers for you.
As there's no GPS-only location priority, you can't request that you only need to turn on GPS, but leave network location off. You can request for network-only location mode by setting your location priority to either PRIORITY_BALANCED_POWER_ACCURACY or PRIORITY_LOW_POWER.
Why we need to go setting for on/off GPS on the other hand we can on/off WIFI and Bluetooth programmatically without move to settings.
Android Guidelines have changed above version 4.0. You cannot change GPS off on programmatically for versions above 4.0.
There used to be a way to enable / disable GPS programmatically by sending the android.location.GPS_ENABLED_CHANGE broadcast:
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", enabled);
sendBroadcast(intent);
where enabled would be true or false respectively.
If you take a look at this bug report, this hack was subverted in Android 4.4. It still works on older OS versions.
Now the answer to your question
Why we need to go setting for on/off GPS on the other hand we can
on/off WIFI and Bluetooth programmatically without move to settings ?
Android's GPS technology periodically sends location data to Google even when no third-party apps are actually using the GPS function. A lot of people are very sensitive about things like real-time location monitoring . That's why Google made it mandatory to get the user's consent before using the GPS function. The following dialog is seen whenever the user turns GPS on:
And hence it is no longer possible to programmatically change the GPS settings, as by necessity it requires the user's permission. What the programmer can do is direct the user to the GPS settings by calling
startActivity(context, new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
and let the user make a choice.
As an interesting point, if you try sending the GPS_ENABLED_CHANGE broadcast on the new OS versions, you get a
java.lang.SecurityException: Permission Denial:
not allowed to send broadcast android.location.GPS_ENABLED_CHANGE
error. As you can see, its a SecurityException with a permission denial message.
The premise of your question is no longer correct. With Google Play Services 7, you can display a dialog to change the location provider settings from within your app. Jump to 1:10 in this video.