Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Geofence gives ApiException status 13

Recently run into a problem where adding geofences will randomly fail with an ApiException, status code 13 which is defined as "ERROR" no extra details provided.

Normally the error codes are specific to geofencing, but this seems to be a generic failure. Has anyone got any idea why the GeofencingClient would return status code 13? I've been unable to find anyone having the same issue.

This seems to be affecting older builds of the app where it was working without issue previously.

I've double checked the latitude/longitude are valid coordinates, and the same data will sometimes work without issue. I've tried initialising with different API keys in case the maps/location services had an issue, but that made no difference.

I've also tried changing from GeofencingClient(context) to LocationServices.getGeofencingClient(context) with no change.

I've tried upgrading the library versions, no difference.

Typical manifest setup with play services version and API key defined;

<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version"/>

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/maps_api_key"/>

<service android:name=".GeofenceTransitionsIntentService"/>

The geofences are added here, and no results ever get to the intent service

private val geofencingClient = GeofencingClient(context)

private val geofencePendingIntent =
            PendingIntent.getService(context, 0, Intent(context, GeofenceTransitionsIntentService::class.java), PendingIntent.FLAG_UPDATE_CURRENT)

fun setGeofence(latitude: Double, longitude: Double, radius: Float, geofenceId: String) {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            val geofenceList = listOf(buildGeofence(latitude, longitude, radius, geofenceId))
            geofencingClient.addGeofences(getGeofencingRequest(geofenceList), geofencePendingIntent)?.run {
                addOnFailureListener {
                    // Failed to add geofences
                    geofenceErrorListener?.invoke("Failed to add Geofence: ${it.message}")
                }
            }
        }
    }

This hasn't been an issue until quite recently, and now it's happening almost 100% of the time.

com.google.android.gms.common.api.ApiException: 13:

Edit: As of PlayServices version 17.7.85 (on my device) the issue seems to be resolved on Google's end

like image 222
Ldawg Avatar asked Jun 19 '19 02:06

Ldawg


People also ask

What is the limit on the number of geofence for each app?

You can have multiple active geofences, with a limit of 100 per app, per device user. For each geofence, you can ask Location Services to send you entrance and exit events, or you can specify a duration within the geofence area to wait, or dwell, before triggering an event.

How do I enable GEO fencing?

Launch the Arlo Secure app on your device. Tap Mode > preferred Arlo device > Geofencing. The Arlo Secure app Would Like to Use your Location prompt displays. Tap OK or Allow.

What is geofence API?

The geofencing API allows you to define perimeters, also referred to as geofences, which surround the areas of interest. Your app gets a notification when the device crosses a geofence, which allows you to provide a useful experience when users are in the vicinity.

How do you stop geofences?

iOS / AndroidGo to Settings -> Automatic Status Updates -> Geofences to view the list of items to manage.


2 Answers

I was doing some testing with the Android Q Beta and noticed that I would always get this error if my app had the new "Allow only while using the app" location permission. Even when the app was in the foreground, I was never able to add any Geofences with the location permission set to "Allow only while using the app" but as soon as I set my app's location permission to "Allow all the time" I can add Geofences no problem.

I am still seeing the problem on other API versions however, so unfortunately it doesn't really solve the problem entirely. I have a device running Android 8.0 and one running Android 9.0 and I still occasionally see the Status Error #13. I am not sure what to do on these devices because they don't have the new "Allow only while using the app" permission. They both have full location access.

I am adding this answer because it might help some users that are running the Android Q Beta or at least shed some light on what may be causing the problem. I do think it has something to do with the latest Google Play Services version because I think we are all doing everything right.

EDIT: I have added an issue on Google's Issue Tracker for this if anyone is interested in following... https://issuetracker.google.com/issues/135771329

like image 56
Stephen Ruda Avatar answered Sep 22 '22 08:09

Stephen Ruda


If you are using API 29 or android 10 phonethi is the case. You need to declare in the manifest the use of:

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

And, he you request permissions you have to ask for both:

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

or

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
like image 41
Deneb Chorny Avatar answered Sep 18 '22 08:09

Deneb Chorny