geofencingEvent.geofenceTransition is always -1

Viewed 21

I'm trying to implement geofencing as mentioned here: https://developer.android.com/training/location/geofencing

I get my BroadcastReceiver's onReceive called, but then a weird thing happens, geofencingEvent.geofenceTransition is always -1, instead of Geofence.GEOFENCE_TRANSITION_ENTER. In any given time i have only 1 geofence. Tested on several devices and emulators. On real devices used Lockito app to simulate movement, on emulators used it's own control panel to change location.

CODE:

RECEIVER:

class GeofenceBroadcastReceiver : BroadcastReceiver(),KoinComponent {

val navInManager:NavInManager = get()

override fun onReceive(context: Context, intent: Intent) {
    Timber.d("***** GeofenceBroadcastReceiver onReceive")
    val geofencingEvent = GeofencingEvent.fromIntent(intent)
    if (geofencingEvent.hasError()) {
        val errorMessage = GeofenceStatusCodes
            .getStatusCodeString(geofencingEvent.errorCode)
        Timber.e("***** error $errorMessage")
        return
    }
    val geofenceTransition = geofencingEvent.geofenceTransition
    if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
        val triggeringGeofences = geofencingEvent.triggeringGeofences
        Timber.i("***** ENTER triggered id ${triggeringGeofences[0].requestId}")

    } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
        val triggeringGeofences = geofencingEvent.triggeringGeofences
        Timber.i("***** EXIT triggered id ${triggeringGeofences[0].requestId}")
        
    } else {
        Timber.e("***** invalid_type $geofenceTransition")
      
    }
}

}

CREATING THE GEOFENCE:

private val geofencePendingIntent: PendingIntent by lazy {
    val intent = Intent(App.instance, GeofenceBroadcastReceiver::class.java)
    PendingIntent.getBroadcast(App.instance, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
}

private fun getGeofencingRequest(geofence: Geofence): GeofencingRequest {
    return GeofencingRequest.Builder().apply {
        setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
        addGeofence(geofence)
    }.build()
}

fun setGeofenceForPoint(stopPoint: StopPoint){       
    val sessionId = "DELIVERY"
    Timber.d("***** before Geofence $sessionId")
    val geofence =
        Geofence.Builder()
        .setRequestId(sessionId)
        .setCircularRegion(
            stopPoint.location.latitude,
            stopPoint.location.longitude,
            1000F
        )
        .setExpirationDuration(1800000)
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
        .build()

    if (ActivityCompat.checkSelfPermission(
            GetPackageCourierApp.instance,
            Manifest.permission.ACCESS_FINE_LOCATION
        ) == PackageManager.PERMISSION_GRANTED
    ) {
        geofencingClient?.addGeofences(getGeofencingRequest(geofence), geofencePendingIntent)?.run {
            addOnSuccessListener {
                Timber.d("***** Geofence(s) added")
            }
            addOnFailureListener {
                Timber.e("***** Failed to add geofence(s)")
            }
        }
    }
}
0 Answers
Related