27

I've been testing the geofencing api available in the latest Google Play Services, using the official sample available here: https://github.com/googlesamples/android-Geofencing/ And of course, following the training course: http://developer.android.com/training/location/geofencing.html

The sample displays a notification when you are nearing two Google buildings in San Fransisco, so I tweaked it to be triggered when I'm nearing my home or my work place.

If I use a real device, it works. The notifications are triggered properly. But when I'm using a simulator, and change the location with telnet or ddms, nothing happens. I also tried with Genymotion, with the Gapps installed, and same stuff: nothing.

Would anyone have any pointer about how to test geofence, without actually moving my butt? Because in the street, there is no way to debug the code...

Redwarp
  • 3,221
  • 3
  • 31
  • 44
  • 11
    Lol at "without actually moving my butt?" – Reed Mar 27 '15 at 00:18
  • Have you tried mocking your location? I recommend two apps: [Fake GPS location](https://play.google.com/store/apps/details?id=com.lexa.fakegps) and [Fake Location Spoofer Free](https://play.google.com/store/apps/details?id=com.incorporateapps.fakegps.fre) (second one is working fine with Google Play Services). – Ziem Mar 29 '15 at 19:14
  • Well, using fake GPS location or another location "mocker" will not help me test everything, because even if it was working (have to try it stil) it requires to switch app, and it means I can't really test what happens with my app in the foreground. – Redwarp Mar 30 '15 at 08:56
  • @Redwarp Checkout my answer, and please mark it as answer so if anybody else has the same problem he can see it – Sid Apr 01 '15 at 12:21
  • @Assa I don't think your answer is the answer I seek, and I don't think toobsco42 would be glad if I gave you the 400 point bounty, for such a short answer. Mocking location seems to work. But it doesn't trigg the geofence – Redwarp Apr 01 '15 at 14:37
  • @Redwarp what you say it's not true at all! Usually the fake location apps run a service that spoofs your locations so you can test your app in foreground just fine! For example with [LocationSpoofer Pro](https://play.google.com/store/apps/details?id=org.ajeje.locationspooferpro) you can spoof a route between two coordinates! – philtz Apr 02 '15 at 13:15
  • @philtz good to know, I will try that. – Redwarp Dec 08 '15 at 09:01

6 Answers6

12

Here is what you need to do:

  1. Settings -> Developer options -> Allow mock locations

  2. Add the following permission:

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

  1. Ensure your GPS location service is ON (high accuracy) not Network location

  2. Download an app called Fake GPS (it has yellow happy fact holding a remote control) from the app store. You can set your fake GPS using that app

Hope this answers you question :)

Sam
  • 4,475
  • 2
  • 28
  • 31
Snake
  • 14,228
  • 27
  • 117
  • 250
  • Your answer is basically the same as the one from @araks, and he gives more details ^_^ thanks anyway. – Redwarp Apr 02 '15 at 14:25
  • He only mentioned point 4 through an adb. I wanted to ensure that you have everything setup and don't have to go through adb... I guess you didn't try my answer or find it helpful. Ok – Snake Apr 02 '15 at 14:56
  • Oh, sorry, I didn't mean to sound harsh or anything, any help is appreciated, really. Just wanted to explain why I wouldn't "pick" your answer as "the one". – Redwarp Apr 03 '15 at 12:03
  • No worries.. Understood. Offcourse pick the answer that solves your problem. Good luck – Snake Apr 03 '15 at 21:53
  • These steps worked for me, it's easy to forgot to include a step in the setup. I used Lockito though, like others mentioned. Lockito has a persistent notification, so I can run my app in the foreground, pull down the notification bar, and hit "Play" from the Lockito notification to start a simulated route. My app in the foreground can see the "movement" fine. – Robert Sep 17 '15 at 06:08
  • Ok, this is for testing geofence on real device what is actually better for my case. Tnx. – Dezo Aug 04 '19 at 19:14
9

I had the same issue, if you have a device you can easily fake the location by enabling mocking location in the settings

Settings-> Developer options-> Allow mock locations

And you also have to add this in the manifest:

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

I never tried on the emulator, but try to open the emulator and do the same things (go to settings and enable mock locations), good luck!

Sid
  • 14,176
  • 7
  • 40
  • 48
  • 2
    So I understand you can allow mock locations in the developer options, but how do you simulate something like a gpx route on a device? – Etienne Lawlor Mar 30 '15 at 17:50
  • How to test this on dose mode? – Rohit Oct 01 '18 at 06:18
  • 1
    when adding the mock location permission, I get the following error: Permission is only granted to system apps. – Rule Jan 17 '19 at 05:57
  • 2
    @Rule you should override the Manifest within the debug or test folder and include the permission within that build variant's manifest. From Android Studio: "Mock locations should only be requested in a test or debug-specific manifest file (typically src/debug/AndroidManifest.xml)" – Payne Miller Feb 21 '19 at 15:45
6

I nearly gave up on testing the geofences...

without actually moving my butt

... but with intensive research i found a solution (it's not optimal though).

The problem with geofences in contrast to regular location updates is that geofences need a little "poke" in order to work.

Geofences rely on other apps and the OS. If there is no app that actively requests location updates and the android system "thinks" it's at the same spot (that's always the case with the emulator) the fences won't trigger.

So there are two solutions:

  • actively request location updates during debugging

  • open google maps in multi-window mode and click the "my-position-button" after you entered the Lat/Long and clicked send in the emulator settings

Of course the things mentioned in the other answers are required (High Accuracy).

I hope this answer saves some time for other devs :)

Appyx
  • 1,145
  • 1
  • 12
  • 21
  • This is the one that worked for me! I had to Sign In on the Google Maps app and then request a location update by tapping the "my-position-button". – Hashim Akhtar Aug 16 '19 at 16:03
4

The app Lockito helps to simulate a route for testing Geofencing. https://play.google.com/store/apps/details?id=fr.dvilleneuve.lockito

You need to turn allow mock locations Settings-> Developer options-> Allow mock locations

I have found that you can set an average speed along the route to speed up the simulation, however if the speed is too quick then Geofencing entering and exiting may not get triggered or be triggered far later than they should. I also set a Geofence radius of at least 40 meters to make sure the triggers happened.

With Lockito if you set a speed that is too fast then the current location (blue dot on the map) won't update fast enough. So first set it to a lower speed then run, then increase the speed and run.

Etienne Lawlor
  • 6,817
  • 18
  • 77
  • 89
  • 3
    Lockito breaks the geofencing on newest versions of Google Play – desgraci Nov 25 '16 at 02:02
  • @desgraci that's true, when I hit Play itinerary my geofences are removed and I get a GEOFENCE_NOT_AVAILABLE call. I had to move to another Fake GPS app(https://play.google.com/store/apps/details?id=com.lexa.fakegps) – Mateus Gondim Dec 01 '16 at 18:46
  • @MateusGondim it didn't work for me since it requires switching apps, and I needed to test on foreground, but thanks :) – desgraci Dec 02 '16 at 00:12
3

I found that the easiest way to set a mock location on the Android emulator is to use a small app designed for this purpose.

When testing apps with location-based features I use Fake GPS location.

You can download the apk from a mirror (search in google for com.lexa.fakegps.apk) and then install it on the emulator with:

adb install com.lexa.fakegps.apk

It works like a charm. Never had more problems setting a fake location like with telnet or DDMS, and it's far more easy since you can use a simple GUI instead of terminal commands!

EDIT: If you need to automatically set location in your unit tests you can use these intents:

Intent startIntent = new Intent();
startIntent.setAction("com.lexa.fakegps.START");
startIntent.putExtra("lat", 56.1234);
startIntent.putExtra("long", 44.0011);
startService(startIntent);

Intent stopIntent = new Intent();
stopIntent.setAction("com.lexa.fakegps.STOP");
startService(stopIntent);

Have a nice testing!

Mattia

JulianHarty
  • 3,178
  • 3
  • 32
  • 46
araks
  • 40,738
  • 8
  • 34
  • 39
  • Only issue I see with using an external app, is that there is no way to test the behavior of your app when it's in the foreground. Because you need to put Fake GPS Location in the foreground to change the location... – Redwarp Apr 02 '15 at 12:49
  • 2
    @Redwarp You can automate your test using this two commands: `am startservice -a com.lexa.fakegps.START -e lat 56.1234 -e long 44.0011` `am startservice -a com.lexa.fakegps.STOP` [link](http://droid-developer.blogspot.it/2012/09/fake-gps-faq-ive-got-lot-of-messages.html) – araks Apr 02 '15 at 12:52
3

I've been 2 days trying to figure out how to test my geofence app on Android Studio Emulator and finally did it. The device had default Play Services (I mean I created the virtual device with it).

I solved it going to "Google Settings" > Location > Mode

And changing it from "Device only" to "High Accuracy".

This way my app finally worked. Hope it helps.

algarrobo
  • 304
  • 1
  • 12