1

I want to test a GPS application I am developing in my android tablet, so I need to send to it spoof locations from a KML file loading it using DDMS. I want to use a physical device, not the emulator.

The problem is that the Emulator Control section in DDMS is disabled (grayed out) when I select my external device (the app process running) in the devices view. Insted if I select the emulator then the emulator control appears enabled but not with the physical device.

My applicaton has

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

And my physical tablet is configured with:

  • USB debugging.
  • Allow mock locations

I'm using SDK r20 and my tablet has android 4.0.3.

Any ideas?

Thank you very much

Serruxo
  • 41
  • 1
  • 5
  • I have worked extensively with GPS applications and I never managed to achieve sending mock locations from the DDMS to a physical device. Instead I added the KML (or other file format) to the app resources and wrote a parser to read the data from the file and inject it into my application in place of the Location updates for testing purposes. – Maurice Gavin Jul 21 '12 at 10:32
  • Thanks for your reply. I was thinking in your idea. Do you know any example that I could use? – Serruxo Jul 21 '12 at 10:50

2 Answers2

2

I've pieced together a solution to this problem.

  • Go to Settings->Applications->Development and select "Allow mock locations".

  • Add ACCESS_MOCK_LOCATION permission to AndroidManifest.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">
    
  • Implement a class which uses the LocationManager.addTestProvider() function. This will indicate to the application that it should use data from a file to construct new Location objects.

  • New Locations can then be created using the LocationManager.setTestProviderLocation() function.

    // start using mock locations
    try {
       mockLocationCreator = new MockLocationCreator(this.getApplicationContext());
       try {
           mockLocationCreator.openLocationList();
    
           mockLocationThread = new Thread(mockLocationCreator);
           mockLocationThread.start();
    
           Toast.makeText(this.getApplicationContext(), 
                          "Mock locations are in use", 
                          Toast.LENGTH_LONG)
                .show();
    
       } catch (IOException e) {
           Toast.makeText(this.getApplicationContext(), 
                          "Error: Unable to open / read data file", 
                          Toast.LENGTH_LONG)
                .show();
           mockLocationCreator = null;
       }
    } catch(SecurityException e) {
       Toast.makeText(this.getApplicationContext(), 
                      "Error: Insufficient Privileges", 
                       Toast.LENGTH_LONG)
            .show();
       Log.e(TAG, "unable to use mock locations, insufficient privileges", e);
    }
    

Note: It is not possible to send mock locations to a real device from DDMS->Emulator Control->Location Controls regardless of device or manifest permissions as is incorrectly suggested here.


Sources:

Android mock location on device? - information about manifest permissions and alternative solution using the telnet command line, links and code snippets.

Using Mock Locations in Android - more verbose, contains some dead links.

LocationManager Documentation - Official Android documentation

Community
  • 1
  • 1
Maurice Gavin
  • 2,047
  • 1
  • 20
  • 26
0

For simple spoofing you can use this free version but as you said in your question you want spoof locations from a KML file for that you have to buy pro version

Akram
  • 7,548
  • 8
  • 45
  • 72
  • Thanks. I tested that pro version and seems not to read very properly a little KML file that I created with the last version of Google Earth. I'm not sure if there are different versions of the KML files, so that app might have a compatibility problem. – Serruxo Jul 21 '12 at 10:51
  • you are welcome i never tested the pro version just read the description once and i saw your question and thought it might help you so i shared it with you. – Akram Jul 21 '12 at 10:55
  • I also tested this [app](https://play.google.com/store/apps/details?id=com.forgottenprojects.mocklocations) that reads KML routes but now there is a issue with the KML file I'm using. The developer said me that is finishing a new corrected version. – Serruxo Jul 21 '12 at 11:07
  • okay but for now you can test your application by entering lat lon in the spoofer.I went through some tutorial and found If you have trouble with a particular KML file, make sure it has this: . – Akram Jul 21 '12 at 11:10
  • The [application](https://play.google.com/store/apps/details?id=com.forgottenprojects.mocklocations) I said, has been updated fixing the last issues with KML files. I tested the last version with a little KML file created with the last version of Google Earth and the results were succesful. I could simulate an entire route. – Serruxo Jul 23 '12 at 19:50