2

FusedLocationProviderClient always returns false in onLocationAvailability callback on Sony Xperia Z2. Permissions are granted, location is enabled. In location preferences set up option to use wifi, network and gps. Everything works fine on emulator, Samsung, BQ, Nexus, Xiaomi. OS versions from 6 to 8. But on Sony (andorid 6.0.1) I have this problem. Any ideas ? I'm over

Callback

private val mUserLocationCallback = object : LocationCallback() {

    override fun onLocationAvailability(locationAvailability: LocationAvailability?) {
        super.onLocationAvailability(locationAvailability)
        val isAvalable = locationAvailability?.isLocationAvailable ?: false
        if (!isAvalable) {
            mLocationHelperListener.onLocationUnAvailable()
            mDefaulLocation?.let { mLocationHelperListener.onLocationHelperChanged(it) }
            stopLocationUpdates()
        }
    }

    override fun onLocationResult(locationResult: LocationResult?) {

        if (locationResult != null) {
            for (location in locationResult.locations) {
                mPreferences?.lastLatitude = location.latitude.toFloat()
                mPreferences?.lastLongitude = location.longitude.toFloat()
                mLocationHelperListener.onLocationHelperChanged(location)
                stopLocationUpdates()
                break
            }
        }

    }
}

Request

val locationRequest = LocationRequest.create()
                    .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                    .setInterval(UPDATE_INTERVAL)
                    .setFastestInterval(FASTEST_INTERVAL)
                    .setSmallestDisplacement(SMALLEST_DISTANCE)


            // Create LocationSettingsRequest object using location request
            val builder = LocationSettingsRequest.Builder()
            builder.addLocationRequest(locationRequest)
            val locationSettingsRequest = builder.build()

            val settingsClient = LocationServices.getSettingsClient(mContext)
            val task = settingsClient.checkLocationSettings(locationSettingsRequest)
            task.addOnSuccessListener { locationSettingsResponse: LocationSettingsResponse? ->
                mFusedLocationClient.requestLocationUpdates(locationRequest, mUserLocationCallback, null)
            }
            task.addOnFailureListener { exception: Exception ->
                mLocationHelperListener.onLocationUnAvailable()
            }
Marriage
  • 501
  • 3
  • 15

2 Answers2

1

I had a very similar issue; a restart of my phone resolved the problem. I was testing on several devices but the only problematic device was a ZTE Maven Z812.

user2821647
  • 407
  • 2
  • 15
-1

I think found solution. I think my solution will work for other devices with same problem. So... Go to application list, find Google PLay Services. Open permissions. If "Location" permission turned off - switch on it. If there is no such item, switch off ALL items, and on it again. You should see dialog for Location permission. Enable it and voila, everything is working now in my case.

Marriage
  • 501
  • 3
  • 15