Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 12 issues #59

Closed
HarelM opened this issue Dec 18, 2021 · 31 comments · Fixed by #63
Closed

Android 12 issues #59

HarelM opened this issue Dec 18, 2021 · 31 comments · Fixed by #63
Assignees
Labels
android Related to the Android platform bug Something isn't working

Comments

@HarelM
Copy link
Collaborator

HarelM commented Dec 18, 2021

Describe the bug
Location is not working well for android 12.
I'm still not sure what and why and this requires more investigation.
The only lead I have is this article:
https://developer.android.com/training/location/permissions#approximate-request

To Reproduce
Not sure...

Expected behavior
GPS should work

Desktop (please complete the following information):

  • OS: [e.g. iOS] android 12

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6] pixel 4a
@HarelM
Copy link
Collaborator Author

HarelM commented Dec 18, 2021

apache/cordova-plugin-geolocation#235
Might be relevant.

@DanielLedger
Copy link

DanielLedger commented Jan 9, 2022

Presumably related to this issue, I have been encountering similar problems with an app I am trying to create:

  • When the app is in the foreground initially, location updates as expected (Although I haven't yet tested if leaving the app in the foreground for an extended period of time will keep the location updating as expected).
  • As soon as the app gets put in the background, the location stops updating.
  • If the location does update for any reason, the resulting location will be snapped to a nearby point, and returned with an inaccuracy of either 899.999, 699.999 or 600 metres.
  • If the user uses another app which requests location in the foreground (tested with Google Maps and OsmAnd) or spoofs the user's location (tested FakeGPS only) then the background location will also update while the user is using the app (still need to test if this works with an app which loads the location in the background such as Google Map's location share feature).

In addition to ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION, my app also has ACCESS_BACKGROUND_LOCATION, with the location permission being set to "Allow all the time".

This setup worked perfectly on Android 11, it has only broken with Android 12.

Phone info: Pixel 3a, Android 12 build SQ1A.220105.002
Build machine info: Linux 5.11.0-44-generic #48~20.04.2-Ubuntu SMP Tue Dec 14 15:36:44 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Hopefully this information helps, if you need specific logs or anything testing let me know.

EDIT: The config being used:

locationProvider: BackgroundGeolocation.DISTANCE_FILTER_PROVIDER,
desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
stationaryRadius: 5,
distanceFilter: 5,
notificationTitle: 'text',
notificationText: 'text',
startForeground: true,
debug: true,
interval: 3000,
fastestInterval: 3000,
activitiesInterval: 3000

@HarelM
Copy link
Collaborator Author

HarelM commented Jan 9, 2022

Can you share logcat of this issue and the plugin's logs?
I've seen this issue mainly with pixel devices, it's not a general problem in android 12.
Unfortunately, I don't have a pixel device to further investigate.
Any help would be appreciated!!

DanielLedger added a commit to DanielLedger/fugitive-game that referenced this issue Jan 13, 2022
Seems to be having issues on Android 12 apparently
(HaylLtd/cordova-background-geolocation-plugin#59)
I don't really have any alternative so I should probably look into it myself.
@DanielLedger
Copy link

I did some further experiments with this issue, and have the following logs
trimmed.log:

These were generated with this app: https://github.com/DanielLedger/debugApp

In addition, although I forgot for the line generated by navigator.geolocation, I also have pictures of the traces generated by the second two tests (running the plugin with OSMAnd also logging the trip, and just the plugin running)

Test 1 (with OSMAnd):
Screenshot_20220113-123743

Test 2 (without OSMAnd):
Note that this was not the same walk, I was following the blue path that goes around the edge of the common (https://www.openstreetmap.org/way/3431516#map=16/50.9313/-1.4116)
Screenshot_20220113-125423

Hopefully this helps!

@HarelM
Copy link
Collaborator Author

HarelM commented Jan 13, 2022

I was expecting not to see anything on the map...
Can you pull the logcat from the device and see if you see anything strange when not using osmand in the background?

@HarelM
Copy link
Collaborator Author

HarelM commented Jan 15, 2022

I've upgraded my device to android 12 and I see similar issues.
When using GPS test it "fixes" the accuracy issue.
If you find a solution before I do key me know...

@HarelM
Copy link
Collaborator Author

HarelM commented Jan 15, 2022

I'm using the raw provider.
I think both raw and distance are not using the fused location.
I have a hunch this might be the case.
I need to check... The activity provider is using fused, which might help narrow down the issue.
If you try it out and let me know it would be great.
It's not a solution but a way to narrow down the issue...

@HarelM
Copy link
Collaborator Author

HarelM commented Jan 15, 2022

I've looked at the GPS Test code, it doesn't use the fused location provider but rather the gps provider.
This plugin uses the location manager getBestProvider using criteria.
It might be that android 12 updated this code to return a provider which is less accurate and more battery optimized.
I'm looking into this issue in the following branch:
https://github.com/HaylLtd/cordova-background-geolocation-plugin/tree/59-android-12-issue
I've added logging about which provider I get when asking for the best one.
For android 11 I get gps
and for android 12 I get fused
I'm still unsure how to solve this, but at least I know where the change is coming from.

@HarelM
Copy link
Collaborator Author

HarelM commented Jan 15, 2022

I've opened a ticket to android since I can reproduce this in their emulator:
https://issuetracker.google.com/issues/214568820
As a workaround, I've hardcoded gps instead of using the criteria for the raw provider, I'm not sure I have a better solution.
Relevant changes can be seen in the branch above.

@DanielLedger
Copy link

I can confirm that the workaround solved the problem on a physical device. It's not ideal but it seems to work.

HarelM added a commit that referenced this issue Jan 19, 2022
@HarelM HarelM added android Related to the Android platform bug Something isn't working labels Jan 19, 2022
@HarelM HarelM self-assigned this Jan 19, 2022
@HarelM
Copy link
Collaborator Author

HarelM commented Jan 19, 2022

@DanielLedger Feel free to review the PR I just opened.
It's less hard coded and I feel OK with it given the bug in the Android OS.
Basically it only applies the hard coded change for devices that has a GPS and are Android 12 or later.
Once merged I can release a new version to avoid the need to use the branch and git reference.

HarelM added a commit that referenced this issue Jan 20, 2022
* Move distance location provider

* Fix package name after movement

* Added logging for provider selection

* Hard code gps

* Fixes #59 - Use GPS provider on android 31.

* Fix hard coded parameter
HarelM added a commit that referenced this issue Jan 20, 2022
* Move distance location provider

* Fix package name after movement

* Added logging for provider selection

* Hard code gps

* Fixes #59 - Use GPS provider on android 31.

* Fix hard coded parameter
@HarelM
Copy link
Collaborator Author

HarelM commented Apr 24, 2022

The response in the issue tracker says that the API was deprecated but was not marked as such.
They also advise to use Google fused location provider and asked why we don't use it.
In any case, no further actions is planned here as far as I'm concerned.

@mackrof
Copy link

mackrof commented Jun 18, 2022

Unfortunately, I still have inaccurate location detection without another application active.

@mackrof
Copy link

mackrof commented Jun 19, 2022

image
image
the same route

@HarelM
Copy link
Collaborator Author

HarelM commented Jun 19, 2022

Can you check if you see the following lines in the log messages of the plugin:
Requesting location updates from provider.
If so, which provider does it use?
Also which provider are you configuring? distance? raw? something else?

@mackrof
Copy link

mackrof commented Jun 20, 2022

android6.txt
android12.txt
logs of identical routes from 2 devices.

configure
image

@HarelM
Copy link
Collaborator Author

HarelM commented Jun 20, 2022

I see in the logs that fused location is used, which is the cause of these inaccurate readings.
Might still be a bug in distance filter provider.
See the last fix I did there:

@mackrof
Copy link

mackrof commented Jun 20, 2022

I have the latest version of the plugin installed, but the error remains.

@HarelM
Copy link
Collaborator Author

HarelM commented Jun 21, 2022

Yup, there's an if there that might cause the plugin to not use GPS provider, and use fused location.
I'm not using filter distance provider, so I can't test fixes I make.
I suggest to try and fork, change the code in the linked file, and once it is working as expected, send a PR.

@HarelM HarelM reopened this Jun 21, 2022
@mackrof
Copy link

mackrof commented Jun 21, 2022

I have removed all the conditions. I left only the GPS provider. It still doesn't accurately show the location.
I switched the provider to RAW, but that didn't help either.

@HarelM
Copy link
Collaborator Author

HarelM commented Jun 21, 2022

Weird... If this doesn't work well with RAW provider then I don't have a good idea what's wrong.
The following is my code, I don't know if it can help...
https://github.com/IsraelHikingMap/Site/blob/1513d6095ede4e9ccb701eaf7dbeee386f912b1e/IsraelHiking.Web/src/application/services/geo-location.service.ts#L124
The only suggestion I have is to debug it and see why do you get low accuracy/different provider/bad readings...

@mackrof
Copy link

mackrof commented Jun 21, 2022

YES! I used your settings and everything worked. Maybe I didn't specify the interval or distanceFilter correctly, it's hard to say yet. But nevertheless, everything works. Thank you very much!
image

@HarelM HarelM closed this as completed Jun 22, 2022
@escully27
Copy link

escully27 commented Jul 17, 2022

The above settings (https://github.com/IsraelHikingMap/Site/blob/1513d6095ede4e9ccb701eaf7dbeee386f912b1e/IsraelHiking.Web/src/application/services/geo-location.service.ts#L124) work for leaving the app open and recording accurately, however, I have been completely unsuccessful while getting background location updates if the device is locked with Android 12 (PIXEL 4 XL).

Here are the config options that worked while locked for Android 11:

BackgroundGeolocation.configure({ locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER, desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY, stationaryRadius: 1, distanceFilter: 1, notificationTitle: 'Trackster', notificationText: 'Your GPS location has been synced in the background & is ready!', notificationIconColor: "#3a3a3a", notificationIconLarge: "ic_stat_iconandroid", notificationIconSmall: "ic_stat_iconandroid", debug: false, interval: 5000, stopOnStillActivity: false, fastestInterval: 5000, activitiesInterval: 10000, activityType: 'Fitness' });

What's interesting is that with any config options I've tested for android 12, I've been also totally unable to even get the notification that background tracking is enabled. again, with android 11 it totally works as described.

I have seen this error in the terminal when instantiating the background geo config. I recently solved a different issue with a different plugin that had the same issue.

2022-07-17 13:38:24.698 2930-2930/com.trackster.main.app E/com.marianhello.bgloc.service.LocationServiceImpl: processCommand: exceptionjava.lang.IllegalArgumentException: com.trackster.main.app: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles. at android.app.PendingIntent.checkFlags(PendingIntent.java:375) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458) at android.app.PendingIntent.getActivity(PendingIntent.java:444) at android.app.PendingIntent.getActivity(PendingIntent.java:408) at com.marianhello.bgloc.sync.NotificationHelper$NotificationFactory.getNotification(NotificationHelper.java:77)

Based off the final line of the above error, it seems the FLAG_IMMUTABLE needs to be present somewhere in the Java code for the notification to fire and from what I know the notification needs to be present for the locked device to update.

I'll see if I can change something on the Java side to get this working.

@escully27
Copy link

Yup that did it -

I changed to this line in NotificationHelper.java. To be honest I'm not super familiar with Java and I'm not sure what this change does besides get the Android 12 to work lol if @HarelM could confirm it's safe I could fork or y'all could just update.

            `PendingIntent contentIntent = PendingIntent.getActivity(appContext, 0, launchIntent, PendingIntent.FLAG_IMMUTABLE);

`

@HarelM
Copy link
Collaborator Author

HarelM commented Jul 17, 2022

Can you create a PR so I can better understand exactly what you changed?

@escully27
Copy link

Ok I just submitted a PR with a fork, let me know how that looks. Thanks!

@HarelM
Copy link
Collaborator Author

HarelM commented Jul 18, 2022

I don't see any PR... did you make sure it's against this repo and not the parent one?

@escully27
Copy link

I think it's here: HarelM#3

@HarelM
Copy link
Collaborator Author

HarelM commented Aug 2, 2022

You need to create a PR against this repo next time :-)
I'll release version 2.0.6 with the immutable flag shortly.

@christiannet
Copy link

christiannet commented Feb 2, 2023

Hi @HarelM. I just upgrade to 2.0.8 version and the issue remains. Any update is retrived in background mode Android 12 API 31. I'm using DistanceFilterProvider

Here is my plugin config:

const config: BackgroundGeolocationConfig = { desiredAccuracy: 5, stationaryRadius: 15, distanceFilter: 10, interval: 60000, stopOnTerminate: false, startForeground: true, notificationTitle: "Hello", notificationText: "Hello" }

@HarelM
Copy link
Collaborator Author

HarelM commented Feb 3, 2023

Can you reproduce this in a simulator?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Related to the Android platform bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants