Beacon detection stops when giving ACCESS_FINE_LOCATION permission in Marshmallow - android

I have set beacon background scan using this tutorial in BaseApplication class but in Marshmallow running device it shows this log:
Caught a RuntimeException from the binder stub implementation.
java.lang.SecurityException: Need ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to get scan results
And finally with this and this reference i was able to give location access for Marshmallow running device to detect beacons.
My Problem:
Even when i give Location access it doesn't detect beacons and also stops to show above Log. Is it the problem as in this ISSUE. My Nexus 5 Build number is MRA58N
UPDATE: When i turn on Location manually now it works. But it's strange. Is it right way to detect beacon?

Android Marshmallow introduces an entirely new spin on application permissions,Users now have the ability to revoke runtime permissions whenever they desire. This means that you can’t assume the app has access to the permission, even if it had been granted previously. You can refer this lib or this guide. And you can create a interface listener location changed after enable GPS, when location != 0. After enable GPS you must resume. I Hope this will help you out.

Related

Scan Bluetooth devices in android studio

I try to write a code from documentation but every time it asks for self-check permission and I did not get any device name
Help me to write a code to scan devices in the area
GitHub repositor link for my application
https://github.com/sachinraj0093/BluetoothAttendance/blob/master/app/src/main/java/com/example/bluetoothattendenceapplication/MainActivity.java
If your device running on Android 11 or lower, you need to check the Location is turned on. And in your code, you lack of calling discovery in case !mbluetoothAdapter.isDiscovering().
You can check returned value when call BluetoothAdapter.startDiscovery() to examine the status for start discovery action.

List Wifi without enabling location service

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

Android 6.0.1 - App details vs App permissions details vary

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.

Android Beacon Library and Update 6.0.1

Yesterday I updated my Samsung Galaxy S5 Neo to the new Android Version 6.0.1. Since then the beacons won't be detected anymore. It does connect the Beacon Service, Updates the scan perios, etc. but it doesn't find any beacons.
Bluetooth is activated and the Beacon scanner of the Beacon Manifacturer still works fine (finds all the beacons). But not my app... It all worked before the new Android Update.
A few things to check:
Verify Location is enabled in Settings -> Location. This must be set to High Accuracy or Battery Saving. If set to Device Only, it won't work. Starting with Android 6, Location must be enabled to do bluetooth scans.
Make sure your app has been granted location permission. Check Settings -> Apps -> Your App -> Permissions, and verify the Location slider is set to on. Starting with Android 6, each app must dynamically request location access at runtime. If your app doesn't have code to do this, you'll need to add it. (Although it is possible to override in settings as described here.)
Make sure your app's manifest declares either FINE_LOCATION or COARSE_LOCATION in its declared permissions.
If the above don't help check LogCat (not just filtering on your application) for bluetooth errors.
See here for more info on the above.

How to get value of app's location permission on Phonegap Android

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

Categories

Resources