Search code examples
androidgoogle-play-servicesandroid-pendingintentandroid-geofence

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


Solution

  • Google got back to us to let us know that they've pushed a fix for this issue. I was able to confirm that my non-working device was now working.