John
John

Reputation: 6278

LocationManager.RequestLocationUpdates takes several minutes to return a result on my phone

I am trying to get my location when my bluetooth device disconnects, but it always takes too long.

[BroadcastReceiver]
[IntentFilter(new[] { BluetoothDevice.ActionAclDisconnected })]
public class BluetoothReceiver : BroadcastReceiver
{
    public async override void OnReceive(Context context, Intent intent)
    {
        Log2("On bluetooth receive");
        Log2("Intent is: " + intent.Action);

        if (BluetoothDevice.ActionAclDisconnected.Equals(intent.Action))
        {
            BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
            string deviceName = device.Name;

            Log2("Device name is: " + deviceName);

            if (device.Name.ToUpper().IndexOf("FOO") != -1)
            {
                Log2("Getting location");
                var loc = await GetLocation();

                if (loc == null || loc.HasValue == false)
                {
                    Log2("Location is null or does not have value");
                    return;
                }

                Log2("Location is: " + JsonConvert.SerializeObject(loc.Value));
            }
        }
    }
}

static Location latestLocation = null;

private async static Task<(double lat, double lon)?> GetLocation()
{
    LocationManager locationManager = (LocationManager)_this.GetSystemService(LocationService);

    string provider = LocationManager.NetworkProvider;

    if (locationManager.IsProviderEnabled(provider) == false)
    {
        Log2("Provider: " + provider + " was not available");
        provider = LocationManager.GpsProvider;

        if (locationManager.IsProviderEnabled(provider) == false)
        {
            Log2("Provider: " + provider + " was not available");
            provider = locationManager.GetBestProvider(new Criteria(), true);

            if (provider == null)
            {
                Log2("Best provider was not available, returning");
                return null;
            }
        }
    }

    Log2("Requested location update and waiting");
    latestLocation = null;

    locationManager.RequestLocationUpdates(provider, 30000, 1, _this);

    Stopwatch sw = new Stopwatch();

    sw.Start();

    while (latestLocation == null && sw.ElapsedMilliseconds < 30000)
        await Task.Delay(100);

    if (latestLocation == null)
    {
        Log2("Latest location was null, returning");
        return null;
    }

    double latitude = latestLocation.Latitude;
    double longitude = latestLocation.Longitude;

    return (latitude, longitude);
}

public void OnLocationChanged(Location location)
{
    Log2("location changed event.");

    latestLocation = location;
    
    Log2($"Latitude: {location.Latitude}, Longitude: {location.Longitude}");
}

When I check the log. It takes over 8 minutes for OnLocationChanged to fire. Its using the NetworkProvider, but I have also tested it using GPS provider. It has this issue when I pull up to my house and my phone disconnects from my car's bluetooth. It doesn't seem to have this issue when testing with my bluetooth headset while at home. Here is the log file:

[2/21/2024 5:24 PM 22674ms] On bluetooth receive
[2/21/2024 5:24 PM 0019ms] Intent is: android.bluetooth.device.action.ACL_DISCONNECTED
[2/21/2024 5:24 PM 0026ms] Device name is: FOO
[2/21/2024 5:24 PM 0021ms] Getting location
[2/21/2024 5:24 PM 0034ms] Requested location update and waiting
[2/21/2024 5:25 PM 42009ms] Latest location was null, returning
[2/21/2024 5:25 PM 0048ms] Location is null or does not have value
[2/21/2024 5:33 PM 507948ms] location changed event.
[2/21/2024 5:33 PM 0015ms] Latitude: 123.2584486, Longitude: 456.1175657

What can I do to get the location within 10 seconds?

Upvotes: 0

Views: 72

Answers (1)

John
John

Reputation: 6278

The issue was that I only had the GPS permissions set to "while using the app" instead of all the time. So it would just hang indefinitely if I ever tabbed over to another app or had the screen off.

Upvotes: 0

Related Questions