Ldawg
Ldawg

Reputation: 143

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

Upvotes: 14

Views: 3528

Answers (8)

Enock Lubowa
Enock Lubowa

Reputation: 717

I got this error when the location access setting for the app was set to Allow only while using the app.

To fix this, go location settings, locate your app and change the setting to Allow all the time

Upvotes: 0

Parth1029
Parth1029

Reputation: 1

Add this permissions into manifest file.

  1. INTERNET
  2. ACCESS_FINE_LOCATION
  3. WAKE_LOCK
  4. ACCESS_COARSE_LOCATION
  5. ACCESS_BACKGROUND_LOCATION

And also try with allowing the permission for location "Every Time" because some times the permission only works while using the application. You should allow permission all the time.

By this you will not get "com.google.android.gms.common.api.ApiException: 13:"

Upvotes: 0

After I changed my compile and targetSdkVersion to 29, I have same problem. First it said com.google.android.gms.common.api.apiexception 13
For this error , I changed all dependencies in build.gradle to newest version.
After that, I caught new error com.google.android.gms.common.api.apiexception 1004
Then I add permission ACCESS_BACKGROUND_LOCATION as above, and finally my app goes well again!

Upvotes: 0

reza rahmad
reza rahmad

Reputation: 1049

in your code intent call permission in android 8 9 10 or above to make geofence work i use dexter

Dexter.withActivity(this@Activity_Map)
        .withPermissions(
                Manifest.permission.ACCESS_COARSE_LOCATION
                ,Manifest.permission.ACCESS_FINE_LOCATION
                ,Manifest.permission.ACCESS_BACKGROUND_LOCATION
        )
        .withListener(object: MultiplePermissionsListener {
            override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
                report?.let {
                    if(report.areAllPermissionsGranted()){
                        aktifkangps()
                        ambilshareddprefnip()
                        geofencingClient = LocationServices.getGeofencingClient(this@Activity_Map)      //drawgeo()
                        cekInternet()
                        with(sharedPreferences) {
                            status = getString(loginmasuk.SP_NIP   , "0").toString()
                        }
                        if (status == "failed"){
                            val imap = Intent(this@Activity_Map, Activity_ambil_lokasi::class.java)
                            startActivity(imap)
                            finish()
                        }
            }

Upvotes: 0

Deneb Chorny
Deneb Chorny

Reputation: 411

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" />

Upvotes: 3

Paul Franceus
Paul Franceus

Reputation: 420

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.

Upvotes: 3

intsoftdev
intsoftdev

Reputation: 11

The addGeofences StatusException: 13 error started happening quite regularly for myself and my app users despite not making any changes to the app. Observed on various devices running Android 9. The users started reporting the issue around mid-June.

After reading about it here - thanks to @Ldawg and @StephenRuda - and adding my comments to the google issue, I decided to make some speculative changes to the app when adding geofences. Did some tests and after the issue wasn't reproducible, released Beta then Production releases to the Play store. The issue hasn't yet been observed with the new version.

I am posting my modified solution in case it helps anyone, however it appears similar conceptually to what Ldawg posted initially with the error. Also I haven't yet been able to regression test the previous version to see if it still occurs as before. Since this is an opaque error with no description it's not inconceivable there are multiple triggers for it caused by different states within gms. Until Google themselves confirm this, the fix is speculative and the issue somewhat elusive.

The basic change in my fix is that before, rxLocationProvider.addGeofences and rxLocationProvider.removeGeofences were called in a loop multiple times for each location and the status 13 error was observed.

After the change, rxLocationProvider.addGeofences is called only once with a list of Geofence objects. rxLocationProvider.removeGeofences also takes a list of locations rather than a single one. The app uses mcharmas android reactive location library https://github.com/mcharmas/Android-ReactiveLocation

Code before with StatusException: 13

private GeofencingRequest createGeofencingRequest(String requestId) {
        Geofence geofence = new Geofence.Builder()
                .setRequestId(requestId)
                .setCircularRegion(stationInfo.getLat(), stationInfo.getLon(), GEOFENCE_RADIUS_METRES)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();
        return new GeofencingRequest.Builder().addGeofence(geofence).build();
    }

public void addGeofence(String locationCrs) {
        final GeofencingRequest geofencingRequest = createGeofencingRequest(locationCrs);
        final PendingIntent pendingIntent = createNotificationBroadcastPendingIntent();

        ArrayList<String> lists = new ArrayList<>();
        lists.add(locationCrs);
        rxLocationProvider
                .removeGeofences(lists)
                .flatMap((Function<Status, Observable<Status>>)
                        status -> rxLocationProvider.addGeofences(pendingIntent, geofencingRequest))
                .subscribe(addGeofenceResult -> {

                }, throwable -> {
                    // **Status 13 here**
                }));
    }

Code after - No errors observed

private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(geofenceList);
        return builder.build();
    }

private Geofence createGeofence(String requestId) {
        return new Geofence.Builder()
                .setRequestId(requestId)
                .setCircularRegion(stationInfo.getLat(), stationInfo.getLon(), GEOFENCE_RADIUS_METRES)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build();
    }

public void addGeofences(List<String> locs) {
        for (String loc : locs) {
            Geofence request = createGeofence(loc);
            geofenceList.add(request);
        }

        final PendingIntent pendingIntent = createNotificationBroadcastPendingIntent();

        disposable.add(rxLocationProvider
                .removeGeofences(locs)
                .flatMap((Function<Status, Observable<Status>>)
                        status -> rxLocationProvider.addGeofences(pendingIntent, getGeofencingRequest()))
                .subscribe(addGeofenceResult -> {

                }, throwable -> {
                    // **No Errors observed**
                }));
    }

Upvotes: 1

Stephen Ruda
Stephen Ruda

Reputation: 585

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

Upvotes: 6

Related Questions