rekabhq / background_locator

A Flutter plugin for updating location in background.
MIT License
287 stars 321 forks source link

Starting Background Locator on Android 12 error (PlatformException('registerLocator' requires the ACCESS_FINE_LOCATION permission., null, null, null)) #339

Open andrewzakhartchouk opened 2 years ago

andrewzakhartchouk commented 2 years ago

I had not noticed this earlier in the year for whatever reason, but on background_locator 1.6.6, Android 12 devices would crash when trying to start the locator. The error/crashes still persist when upgrading to the latest version (1.6.12). Older versions of Android do not have this issue.

I/flutter ( 8473): Error :  PlatformException('registerLocator' requires the ACCESS_FINE_LOCATION permission., null, null, null)
I/flutter ( 8473): StackTrace :  #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
I/flutter ( 8473): #1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
I/flutter ( 8473): <asynchronous suspension>
I/flutter ( 8473): #2      BackgroundLocator.registerLocationUpdate (package:background_locator/background_locator.dart:45:5)
I/flutter ( 8473): <asynchronous suspension>
I/flutter ( 8473): #3      _MenuScreenState._startBackgroundLocator (package:myproject/views/menu_screen.dart:734:12)
I/flutter ( 8473): <asynchronous suspension>
I/flutter ( 8473): #4      _MenuScreenState._startGps (package:myproject/views/menu_screen.dart:515:5)
I/flutter ( 8473): <asynchronous suspension>
I/flutter ( 8473): ---------------------- WRITING TO PATH /storage/emulated/0/Android/data/myproject.android/files

With some poking around, the piece of code that throws this error (found in BackgroundLocatorPlugin.kt) can be commented out to stop causing the error. This is only a temporary fix, but perhaps can help whomever is better versed with this package.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                    context.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                    == PackageManager.PERMISSION_DENIED) {

                val msg = "'registerLocator' requires the ACCESS_FINE_LOCATION permission."
                result?.error(msg, null, null)
                return
            }
iamsahilsonawane commented 2 years ago

@andrewzakhartchouk

Two things that can cause this issue:

  1. Android version should at least be API 23 (Marshmello) or Android 6.
  2. No permission for ACCESS_FINE_LOCATION added in manifest

IMO, most probably it's a permission issue.

To add the permission in the manifest: Add <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> in your android manifest file.

Ex: Check out this file for reference.