I'm trying to let my Android Wear watch face determine the current location of the device. This has worked before but after uninstalling and reinstalling the package, it just fails. I think I did everything right and it has already worked like that. I have no idea what the issue is or even how to catch it. Here's the relevant code:
class MyWatchFaceService : CanvasWatchFaceService() {
override fun onCreateEngine(): Engine {
return Engine()
}
inner class Engine : CanvasWatchFaceService.Engine() {
private lateinit var fusedLocationClient: FusedLocationProviderClient
private var lastKnownLocation: Location? = null
override fun onCreate(holder: SurfaceHolder) {
super.onCreate(holder)
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this@MyWatchFaceService)
updateLocation()
}
fun updateLocation() {
Logger.getAnonymousLogger().info("updateLocation")
try {
fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
if (location != null) {
// Location available, use it and update later
Logger.getAnonymousLogger().info("- location available")
lastKnownLocation = location
}
}
.addOnFailureListener {ex: Exception ->
Logger.getAnonymousLogger().warning("- location NOT accessible (inner): " + ex.toString())
}
} catch (ex: SecurityException) {
// Nothing we can do, no location available
Logger.getAnonymousLogger().warning("- location NOT accessible: " + ex.toString())
}
Logger.getAnonymousLogger().info("updateLocation (end)")
}
}
}
I then find this in the log:
03-22 09:41:59.521 7390-7390/de.unclassified.watchface1 I/null: updateLocation
03-22 09:41:59.536 7390-7390/de.unclassified.watchface1 I/null: updateLocation (end)
03-22 09:41:59.830 7390-7405/de.unclassified.watchface1 E/AndroidRuntime: FATAL EXCEPTION: GoogleApiHandler
Process: de.unclassified.watchface1, PID: 7390
java.lang.SecurityException: Client must have ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission to perform any location operations.
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at com.google.android.gms.internal.zzeu.zza(Unknown Source)
at com.google.android.gms.internal.zzcfa.zzif(Unknown Source)
at com.google.android.gms.internal.zzcfd.getLastLocation(Unknown Source)
at com.google.android.gms.internal.zzcfk.getLastLocation(Unknown Source)
at com.google.android.gms.location.zzg.zza(Unknown Source)
at com.google.android.gms.common.api.internal.zze.zza(Unknown Source)
at com.google.android.gms.common.api.internal.zzbo.zzb(Unknown Source)
at com.google.android.gms.common.api.internal.zzbo.zzaiw(Unknown Source)
at com.google.android.gms.common.api.internal.zzbo.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzac.onConnected(Unknown Source)
at com.google.android.gms.common.internal.zzn.zzakr(Unknown Source)
at com.google.android.gms.common.internal.zze.zzw(Unknown Source)
at com.google.android.gms.common.internal.zzi.zzaks(Unknown Source)
at com.google.android.gms.common.internal.zzh.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.os.HandlerThread.run(HandlerThread.java:61)
03-22 09:41:59.843 7390-7405/de.unclassified.watchface1 I/Process: Sending signal. PID: 7390 SIG: 9
So it does call my updateLocation
function, then leaves again, and then there is an exception. But it's not from calling my code as far as I can see. Where does it come from? It doesn't have a stack trace with source files.
The package already has all relevant permissions, as before, along with some more for other tasks.
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
So what's going on here and why does neither the try/catch nor the failure listener do something?