Android Question [SOLVED] Sanity check: Does the Android emulator work with geofences?

Sandman

Expert
Licensed User
Longtime User
EDIT: Short answer is no. Read post #22 for a bit more info and possible ugly workaround.

I'm having trouble getting the emulator working with geofences. Like, this reference code by @Erel, which works fine on my real phone, doesn't seem to work at all in the emulator. I am pretty new with working with the emulator, so I might have misunderstood something, but I can't figure out what that could be.

It would be great if somebody could help me test that it actually works:
  1. Use the example found at the geofence post
  2. Change the geofence location in the code to something you know where it is
  3. Compile as release and install into the emulator, which also automatically runs the app
  4. Make sure the log in B4A says the app successfully added the geofence
  5. Drop the app into the background
  6. Make sure to NOT launch any other apps that request location (like Google Maps etc)
  7. Open the location settings in the emulator (by pressing the ... icon in the vertical toolbar window to the right)
  8. Set the position to be near your geofence
  9. Wait a couple of minutes, did the app react and say that you entered the geofence?
  10. Set the position to be far away from the geofence
  11. Wait a couple of minutes, did the app react and say that you exited the geofence?

For the record, I'm testing this on a 6 inch emulated device with API30 API32 and Play Store. (I don't have the exact name available at the moment.) I should also mention that I have verified, using Google Maps, that when I do the above, the emulated device actually gets its position updated. So everything seems to work fine, it's just that I can't get the geofences to trigger.


UPDATE

To better explain my situation, here's what I installed from SDK Manager (because having an image with Play store would be more like the average users, I thought)

1647493741474.png



Which looks like this in the AVDManager (I've also tried getting geofences to work using the Platform_28 above, but no success)

1647493792359.png


And this is what the device looks like:

1647493706516.png



If one click this button:

1647493943637.png



A window will show, where one can control lots of stuff with the emulator, such as the location:

1647494457439.png



And, just to give an example, here's a picture showing me setting the location, and Google Maps showing where it thinks I am:

1647494771950.png



So it's possible to provide locations for the emulator just fine, it's just that I can't get geofences to trigger.
 

Attachments

  • 1647493769052.png
    1647493769052.png
    16.8 KB · Views: 180
Last edited:

DonManfred

Expert
Licensed User
Longtime User
I am pretty new with working with the emulator, so I might have misunderstood something, but I can't figure out what that could be.
Does your pc have an GPS-Device? It not then it will not work i guess.
How can Geofencing work without GPS-Position? You can set a fixed location for the emulator i think. But this does not help to get Geofencing to work i fear.
 
Last edited:
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Does your pc have an GPS-Device? It not then it will not work i guess.
You guess wrong. If you're willing to help, I would recommend starting the emulator and reading my full post, please pay extra attention to step 7. The emulator has the feature to set the GPS position for the emulated device.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
My emulator has no GMS service
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
** Activity (main) Resume **
*** Service (geofenceservice) Create ***
** Service (geofenceservice) Start **
com.google.android.gms.common.api.ApiException: 17: API: LocationServices.API is not available on this device. Connection failed with: ConnectionResult{statusCode=SERVICE_INVALID, resolution=null, message=null}
Geofence added: false
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
That's amazing! Did you end up using the image you selected in your post #7?

(And of course the app is compiled in Release mode. :) )

Yes, I also need to install Android SDK Platform 31.

1647498290632.png


1647498451128.png


The notification pops up.
Take note: I added a Log for the transition.

1647498358665.png
 
Last edited:
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Take note: I added a Log for the transition.
Are you sure, the example by Erel already had that...

Also, I can tell that the geofence app is in the foreground, which means that you probably didn't do step 5 in my first post. Would it be possible for you to follow that list exactly and tell me how it worked out?

Note: I installed the same as you, and I can't get geofences to work here either.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
If I put in background, it seems the service get destroyed.
This is as it should be. Geofences doesn't require a service running in the background. Instead the app informs the OS about the geofence and supplies an intent that the OS should use to wake the app with when there is a geofence event.

If I put the app in background, nothing happen even I click the "Set Location" button in "Single points" tab.
Once I open Google Maps app, the notification is triggered.
So you get identical results to what I get - that it doesn't work in the emulator.

Are you planning on leaving your computer today? If so, could I ask you to set a reasonable geofence location and install it as release to your real phone and see if it triggers when you're on the move? (Don't forget to put it into background, so it's a real test.)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
This is as it should be. Geofences doesn't require a service running in the background. Instead the app informs the OS about the geofence and supplies an intent that the OS should use to wake the app with when there is a geofence event.


So you get identical results to what I get - that it doesn't work in the emulator.

Are you planning on leaving your computer today? If so, could I ask you to set a reasonable geofence location and install it as release to your real phone and see if it triggers when you're on the move? (Don't forget to put it into background, so it's a real test.)

I think the emulator is not accurate as it shows a "low accuracy" message on the Google Maps.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Make sure the location permissions for the app looks like this, and then try again?

View attachment 126708
Yes, I already set that.

I suspect because I am inside a building.

Can you provide a modified sample like putting different geofence points. Or I just simply add them in AddGeofence sub like below.

B4X:
Sub AddGeofence
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        Dim p As Phone
        If p.SdkVersion >= 29 Then
            rp.CheckAndRequest("android.permission.ACCESS_BACKGROUND_LOCATION")
            Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        End If
        If Result Then
            Dim geo As Geofence
            geo.Initialize
            geo.Id = "GEO-01"
            'geo.Center.Initialize2(32.83722, 35.26981) 'change location!
            geo.Center.Initialize2(LAT01, LNG01)
            geo.RadiusMeters = 100
            geo.ExpirationMs = DateTime.TicksPerDay 'expire after one day
            CallSubDelayed3(GeofenceService, "AddGeofence", Me, geo)
            Wait For Geofence_Added (Success As Boolean)
            Log("Geofence (GEO-01) added: " & Success)
           
            Dim geo As Geofence
            geo.Initialize
            geo.Id = "GEO-02"
            geo.Center.Initialize2(LAT02, LNG02)
            geo.RadiusMeters = 1000
            geo.ExpirationMs = DateTime.TicksPerDay 'expire after one day
            CallSubDelayed3(GeofenceService, "AddGeofence", Me, geo)
            Wait For Geofence_Added (Success As Boolean)
            Log("Geofence (GEO-02) added: " & Success)
           
            Dim geo As Geofence
            geo.Initialize
            geo.Id = "GEO-03"
            geo.Center.Initialize2(LAT03, LNG03)
            geo.RadiusMeters = 500
            geo.ExpirationMs = DateTime.TicksPerDay 'expire after one day
            CallSubDelayed3(GeofenceService, "AddGeofence", Me, geo)
            Wait For Geofence_Added (Success As Boolean)
            Log("Geofence (GEO-03) added: " & Success)          
        End If
    End If
End Sub
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If I put the app in background, nothing happen even I click the "Set Location" button in "Single points" tab.
Once I open Google Maps app, the notification is triggered.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
I suspect because I am inside a building.
That shouldn't matter to the emulator. Or did you perhaps mean your real phone?

Or I just simply add them in AddGeofence sub like below.
Yep, that should work. But I'm not sure why, because if it doesn't work for one geofence, why would it work for multiple? Or perhaps you just want to make sure you have more opportunities to pass through them, I suppose...

That's an interesting post. I've searched quite a bit about this, but I hadn't found that one. It seems to point to the emulator doesn't actually get the new location until something requests a fresh location. Which is strange... I mean, it's like there's an OS service missing from it, that usually listens for location events...
 
Upvote 0
Top