An input or output trigger does not fire when I use geofencing

I am using geofencing in my android app by following this link .

I create and add a geo object in my default visible activity and listen to its events using the IntentService, as described in the link above.

The Geofence object was successfully added, but when I tested it using a real device and moved it to / from a specific geofence area, its ENTER / EXIT event does not fire.

I tested it with a radius of 100 m, 50 m, 500 m, 1500 m and 2000 m, but it does not work in any of the cases.

This is how I created and added geofencing:

 mGeofencingClient = LocationServices.getGeofencingClient(DashboardActivity.this);

Geofence geofence = new Geofence.Builder()
                // Set the request ID of the geofence. This is a string to identify this
                // geofence.
                .setRequestId(REQUEST_ID)

                .setCircularRegion(
                        22.703617,
                        75.873409,
                        Constants.GEOFENCE_RADIUS_IN_METERS
                )
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER |
                        Geofence.GEOFENCE_TRANSITION_EXIT )

                .build();
        mGeofenceList.add(geofence);

        GeofencingRequest geofencingRequest = new GeofencingRequest.Builder()
                .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER|GeofencingRequest.INITIAL_TRIGGER_EXIT)
                .addGeofences(mGeofenceList).build();

        Intent intents = new Intent(this, GeofenceTransitionsIntentService.class);
        mGeofencePendingIntent = PendingIntent.getService(DashboardActivity.this, 0, intents, PendingIntent.
                FLAG_UPDATE_CURRENT);


        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }else {
            mGeofencingClient.addGeofences(geofencingRequest, mGeofencePendingIntent)
                    .addOnSuccessListener(this, new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Log.e("geofencing success", "successfully added");
                        }
                    }).addOnFailureListener(this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.e("geofencing failure", "failed in added");

                }
            });
        }
Run codeHide result

To listen for ENTER / EXIT events, I use an IntentService that looks like this:

public class GeofenceTransitionsIntentService extends IntentService {

    public GeofenceTransitionsIntentService(String name) {
        super(name);
    }

    protected void onHandleIntent(Intent intent) {
        GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
        Toast.makeText(this,"service get start",Toast.LENGTH_LONG).show();

        Log.e("service","geofencing service started");
        if (geofencingEvent.hasError()) {
            String errorMessage = GeofenceErrorMessages.getErrorString(this,
                    geofencingEvent.getErrorCode());
            Toast.makeText(this,"error_in_geofancing"+errorMessage,Toast.LENGTH_LONG).show();
            return;
        }

        // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();

        // Test that the reported transition was of interest.
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
                geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT || geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL) {

            // Get the geofences that were triggered. A single event can trigger
            // multiple geofences.
            List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences();

            Toast.makeText(this,"you are inside range",Toast.LENGTH_LONG).show();
            // Get the transition details as a String.
            String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition, triggeringGeofences);

            // Send notification and log the transition details.
          sendNotification(geofenceTransitionDetails);
            Log.e("geofancing","you are inside range");

        } else {
            Toast.makeText(this,"Invalid_Transition",Toast.LENGTH_LONG).show();
        }
    }
    private String getGeofenceTransitionDetails(
           // Context context,
            int geofenceTransition,
            List<Geofence> triggeringGeofences) {

        String geofenceTransitionString = getTransitionString(geofenceTransition);

        // Get the Ids of each geofence that was triggered.
        ArrayList <String>triggeringGeofencesIdsList = new ArrayList<>();
        for (Geofence geofence : triggeringGeofences) {
            triggeringGeofencesIdsList.add(geofence.getRequestId());
        }
        String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList);

        return geofenceTransitionString + ": " + triggeringGeofencesIdsString;
    }
    }
Run codeHide result

ACCESS_FINE_LOCATION LOCATION_HARDWARE .

+4

Source: https://habr.com/ru/post/1691170/


All Articles