dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.05k stars 1.73k forks source link

Geolocation.StartListeningForegroundAsync stopps calling event after 2 positions on Android #22683

Open TFreudi1 opened 4 months ago

TFreudi1 commented 4 months ago

Description

I registered an Geolocation.LocationChanged event handler and call than Geolocation.StartListeningForegroundAsync like in the sample. On my phone S24 Android 14 the event is only called 2 times. The GPS symbol is continous "on" so I thing it is something with calling my event handler.

Steps to Reproduce

get the sample from the link, tap "click me to test GPS". It should count every second, but stops after 2.

Link to public reproduction project repository

https://github.com/TFreudi1/MauiGpsTest.git

Version with bug

8.0.3 GA

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

Android, I was not able test on other platforms

Affected platform versions

Android 14

Did you find any workaround?

No

Relevant log output

Not relevant outputs avaible, it is not during building.
TFreudi1 commented 4 months ago

It also happens on an old Android 8 device exactly 2 positions than it stops allthoug the GPS symbol still on on the top of the display. But if I use a simulator(Android 9) with a gpx file to simulate multiple GPS Positions it works.

TFreudi1 commented 4 months ago

Further details... it is'nt completly stop you have to move more than 50 meters to get another position. I only walked arround outside our office. A standard GPS App gives constantly changing positions, but the .NET MAUI App did'nt change. I have in foreground all the time. But as I start goin home by car it counts (means LocatinChanged appears) about every 50-100 m. The App has the correct rights, also "high accuracy location" in the Android 14 phone, the Android 8 Phone dosent have this option but acting the same you have to move more than 50 meters to get a new position.

kevinxufei commented 4 months ago

Verified this issue with Visual Studio 17.11.0 Preview 1.0 (8.0.40/8.0.3/8.0.0-rc.2.9530). Can repro it.

sk1llsh0t commented 4 months ago

I am also experiencing this issue. It fires the locationchanged event once or twice and stops. if i start driving around, eventually i'll get another location event. Even setting minimum time to 3 seconds has no effect.

Stuart88 commented 3 months ago

Happening to me also.

Current workaround is a basic manual polling service to get GeoLocation data only (i.e. no speed, bearing etc that comes with GeoLocation.StartListeningForegroundAsync).

If all you need is location data, it does the trick.

//CustomLocationPoll.cs

 public class CustomLocationPoll
 {

     public event EventHandler<Location> LocationUpdated;

     private CancellationTokenSource cts;

     public CustomLocationPoll()
     {
     }

     public async void StartPolling()
     {
         cts = new CancellationTokenSource();
         while (!cts.Token.IsCancellationRequested)
         {
             try
             {
                 Location location = await Geolocation.Default.GetLocationAsync();

                 if (location != null)
                 {
                     this.LocationUpdated?.Invoke(this, location);
                 }
             }
             catch (Exception ex)
             {
                 // Handle error
             }

             // Delay optional - GetLocationAsync() might have enough delay already.
             // await Task.Delay(5000);
         }
     }

     public void StopPolling()
     {
         cts?.Cancel();
     }
 }
// MauiProgram.cs

builder.Services.AddScoped<CustomLocationPoll>();

Just inject into any class or Razor page and assign a handler to LocationUpdated.

MrZander commented 3 months ago

Is any progress being made towards fixing this? I'd rather not have to fallback to polling again.

daniel-scatigno commented 2 months ago

I can Confirm the same behaviour tested on android plataform 24 package Microsoft.Maui.Essentials 8.0.70 using emulator Android 34, also on my phone Poco M4 pro

elparasite commented 2 months ago

Same issue for me. Samsung S21 Ultra, Android 14, Microsoft.Maui.Essentials 8.0.70.

In most cases only one call of LocationChanged, I tried to keep it in my pocket while moving and I had few more but only 4 calls in 40 minutes on my way back in a building (maybe when GPS tracking is available again).