Send GPS coordinates to the Android emulator using DDMS, telnet or any other means

advertisements

DDMS is not able to send location to the emulator. I have tried sending just the location from DDMS but still the emulator is not able to receive location. Nothing appears on the DDMS log when I click the Send button.

I tried sending geo fix from telnet which returns OK but doesn't actually update the location, or if it does I can't read it via my application.

The application works properly in the device, is able to capture test location details but not able to capture location data sent to the emulator either via DDMS or telnet.

I am testing on Android 2.2 emulator. Can anyone let me know what is wrong?

My app (below) is written in C# using Mono for Android and may need fixing (I'm a newbie to all things Android so I could have missed something). OnLocationChanged(Location location) just doesn't seem to fire at all, as if the listener isn't properly defined. Any help appreciated.

Note: The first time I run this Activity the LocationManager.GetLastKnownLocation is null but the test provider stuff isn't accessed. When I run it again GetLastKnowLocation is still null but the test provider stuff is accessed and set. Weird.

[Activity(Label = "Location Demo")]
public class LocationActivity : Activity, ILocationListener
{
    private TextView _locationText;
    private LocationManager _locationManager;
    private StringBuilder _builder;
    private Geocoder _geocoder;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.LocationActivity);
        _geocoder = new Geocoder(this);
        _locationText = FindViewById<TextView>(Resource.Id.TextView1);
        _locationManager = (LocationManager)GetSystemService(LocationService);

        if (_locationManager.GetLastKnownLocation("gps") == null)
        {
            _locationManager.AddTestProvider("gps", false, false, false, false, false, false, false, 0, 5);
            _locationManager.SetTestProviderEnabled("gps", true);
            Location loc = new Location("gps");
            loc.Latitude = 50;
            loc.Longitude = 50;
            _locationManager.SetTestProviderLocation("gps", loc);
        }
        Location lastKnownLocation = _locationManager.GetLastKnownLocation("gps");

        if (lastKnownLocation != null)
        {
            _locationText.Text += string.Format("Last known location, lat: {0}, long: {1}", lastKnownLocation.Latitude, lastKnownLocation.Longitude);
        }
        else
        {
            _locationText.Text += string.Format("Last location unknown");
        }
        _locationManager.RequestLocationUpdates("gps", 5000, 2, this);
    }
    public void OnLocationChanged(Location location)
    {
        _locationText.Text += string.Format("Location updated, lat: {0}, long: {1}", location.Latitude, location.Longitude);
    }
    public void OnProviderDisabled(string provider){}
    public void OnProviderEnabled(string provider){}
    public void OnStatusChanged(string provider, Android.Locations.Availability availability, Bundle extras){}

}

Kudos to https://stackoverflow.com/users/170333/greg-shackles for getting me this far.


I think the problem may be with how you're calling RequestLocationUpdates(). That third parameter is the minimum distance the device needs to move before you get updates, so you're telling the system to only send updates after the device has moved 2 meters. If it works on a real device, it's probably because you moved more than 6 feet. :)

Try starting with RequestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this). That will start a stream of updates on a real device, but only one when you press 'Send' in DDMS. Once that works, I would work back from there on how often you get updates.

Also, GetLastKnownLocation() is always null when you start the emulator. It's better for devices since it can send you the network location as a starting estimate, or the GPS location if another program was using it recently.

EDIT

It could also be a permissions issue. Normally you need to alter AndroidManifest.xml to get GPS access. The line is

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

See the docs here.