capacitor-community / background-geolocation return D/Capacitor: Location not available

Issue

This Content is from Stack Overflow. Question asked by spardano

I am trying to use capacitor-community / background-geolocation. i think the background runs like it supposed to be. but it didnt return me the locatio. it returns D/Capacitor: Location not available.

i dont know what happen, i think i just set up the requirement for this plugin. but it just wont give me the location…

here is my AndroidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest package="ai.squad.kominfo.sbh" xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">
        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode" android:label="@string/title_activity_main" android:launchMode="singleTask" android:name="ai.squad.kominfo.sbh.MainActivity" android:theme="@style/AppTheme.NoActionBarLaunch" android:exported="true">
            <intent-filter> 
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true" android:name="androidx.core.content.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
        </provider>
        <service
            android:name="com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService"
            android:enabled="true"
            android:exported="true"
            android:foregroundServiceType="location" />
    </application>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-feature android:name="android.hardware.location.gps" />
</manifest>

And String.xml

<!--
        The channel name for the background notification. This will be visible
        when the user presses & holds the notification. It defaults to
        "Background Tracking".
    -->
    <string name="capacitor_background_geolocation_notification_channel_name">
        Background Tracking
    </string>

    <!--
        The icon to use for the background notification. Note the absence of a
        leading "@". It defaults to "mipmap/ic_launcher", the app's launch icon.

        If a raster image is used to generate the icon (as opposed to a vector
        image), it must have a transparent background. To make sure your image
        is compatible, select "Notification Icons" as the Icon Type when
        creating the image asset in Android Studio.
    -->
    <string name="capacitor_background_geolocation_notification_icon">
        drawable/ic_tracking
    </string>

This is how i code it inside my page.ts

import {BackgroundGeolocationPlugin} from "@capacitor-community/background-geolocation";

const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>("BackgroundGeolocation");

  ngOnInit() {
    this.getLatLong();
    if(this.plt.is('capacitor')){
     this.backGroundLocation();
    }
  }

async backGroundLocation() {

         await BackgroundGeolocation.addWatcher({
            backgroundMessage: 'Cancel to prevent battery drain',
            backgroundTitle: "Tracking You.",
            requestPermissions: true,
            stale: false,
            distanceFilter: 50,
          },
          function callback(location, error) {
            if (error) {
              if (error.code === "NOT_AUTHORIZED") {
                  if (window.confirm(
                      "This app needs your location, " +
                      "but does not have permission.nn" +
                      "Open settings now?"
                  )) {
                      // It can be useful to direct the user to their device's
                      // settings when location permissions have been denied. The
                      // plugin provides the 'openSettings' method to do exactly
                      // this.
                      BackgroundGeolocation.openSettings();
                  }
              }
              return console.error('error'+error);
          }

          return console.log('test:'+location);
          
        }
        )
  }

This is my variables.gradle file, in case this is related

ext {
    minSdkVersion = 21
    compileSdkVersion = 30
    targetSdkVersion = 31
    androidxActivityVersion = '1.2.0'
    androidxAppCompatVersion = '1.2.0'
    androidxCoordinatorLayoutVersion = '1.1.0'
    androidxCoreVersion = '1.3.2'
    androidxFragmentVersion = '1.3.0'
    junitVersion = '4.13.1'
    androidxJunitVersion = '1.1.2'
    androidxEspressoCoreVersion = '3.3.0'
    cordovaAndroidVersion = '7.0.0'
}

and here is my MainActivity.java, i read in some website that i need to code something here. but it gave me another error, so i decided not to. cause i think in this android version, Plugin does not have to be added manually inside this file

package ai.squad.kominfo.sbh;
import com.getcapacitor.BridgeActivity;

public class MainActivity extends BridgeActivity {

}

so guys, anyone who knows how to use this plugin, help me please…



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?