2

On Samsung Galaxy s10e (Android 9 and Android 10), the BroadcastReceiver never receives data, that the user has crossed the geofence, although everything works on other devices (tested on Samsung a50, Samsung s8, Samsung s9, etc.). Tried turning off battery optimization, but even in the foreground nothing happens. The logs show that GeofencingClient successfully adds all locations. Perhaps someone faced such a problem?

private fun buildChildGeofencingRequest(geofences: List<Geofence>): GeofencingRequest? {
        return if(geofences.isNotEmpty()) {
            GeofencingRequest.Builder()
                            .addGeofences(geofences)
                            .build()
        } else null
    }


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

private fun buildGeoFence(geofence: GeofenceModel, type: GeoFenceType): Geofence? {

        val latitude = geofence.lat
        val longitude = geofence.lng
        val radius = geofence.radius

        val builder = Geofence.Builder()
                        .setRequestId(geofence.id)
                        .setCircularRegion(
                                        latitude,
                                        longitude,
                                        radius.toFloat()
                        ).setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
                         .setExpirationDuration(Geofence.NEVER_EXPIRE) 

        return builder.build()
    }

MainActivity


    val geofencesRequest: GeofencingRequest? = {
            val geofences = mutableListOf<Geofence>()
            filteredGeofences.forEach { model ->
                //Build Geofence for each Geofence models.
                val geofence = buildGeoFence(model, GeoFenceType.CHILD)
                if (geofence != null) {
                    geofences.add(geofence)
                }
            }
            buildChildGeofencingRequest(filteredGeofencesWithParent)
        }()

        if(geofencesRequest != null) {
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                geofencingClient
                                .addGeofences(geofencesRequest, geofencePendingIntent)
                                .addOnSuccessListener {
                                    Log.d("Child Geofence", "Geofence Added Successfully ${filteredGeofences.map { "${it.id}, " }}")
                                }
                                .addOnFailureListener {
                                    context.clearPresenceData()
                                    Log.d("Child Geofence", "Geofence Addition Failed ${it.message}")
                                }
            }
        }

GeofenceBroadcastReceiver

class GeofenceBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        val geofencingEvent = GeofencingEvent.fromIntent(intent)
        handleEvent(geofencingEvent, context)
    }

    private fun handleEvent(event: GeofencingEvent, context: Context) {

        //Child geofence Enter event
        if (event.geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
Log.d("GEO","USER ENTERED ${event.triggeringGeofences}")
}

0 Answers0