Skip to content

Pointr Mobile SDK - Background Positioning

By default mobile applications avoid using background positioning, since when it is enabled, it consumes significantly more battery power, which can lead to device overheating and can cause users to blame their app for battery consumption. For this reason, the default permissions offered by most applications allow you to enable location only when the application is on foreground. This guide explains how to make positioning work even when the application is running on the background.

Warning

On iOS the GPS accuracy in the background needs to be set between 1 and 4 in order to guarantee that beacons are detected in the background, and therefore indoor positioning works in the background.

Warning

Some SDKs and applications (for example certain advertising network SDKs) misused background positioning in the past. As response to this, manufacturers by default block from their official stores any application using background positioning unless they have a good reason and explanation of that reason for it.

Enabling Background Positioning from the App

For background positioning and geofencing to work on iOS, the following settings need to be executed.

  • Enable “Location Updates” under “Background modes” inside Capabilities on xcode
  • Add “Privacy - Location Always Usage Description” inside Info.plist

iOS allows apps to share location even when the phone is locked, but it depends on the following factors:

  • User Permissions: The app must have the appropriate permissions. When an app requests location access, users can choose between three options:
    • Allow While Using the App: Location access only when the app is open.
    • Allow Once: Temporary access.
    • Allow Always: Location access even when the app is running in the background, including when the phone is locked.
  • App Requirements: The app must be designed to handle background location updates. This is typically used in navigation apps, fitness trackers, and certain safety apps.
  • Battery Optimization: iOS might restrict background activities (including location sharing) to save battery, especially when the phone is locked for extended periods.
  • Privacy Settings: Users can always adjust these settings under the device’s privacy options if they change their mind about an app’s location access.

So, if the app has “Always” permission and is designed for background location updates, it can share the location even when the phone is locked.

let configuration = PTRMutableConfiguration()
    configuration.positionManagerConfiguration.isBackgroundPositioningEnabled = true
    configuration.positionManagerConfiguration.isGpsTrackingEnabledInBackground = true
    Pointr.shared.configurationManager?.updateRuntimeConfiguration(configuration)

Pointr SDK by default includes all the necessary permissions in the manifest to use background positioning. So, from the app side, you only need to request the permissions from the user, which can be done using Pointr API’s, then you should add the configuration parameters to enable background positioning from your Pointr Cloud instance. If you do not want to enable the background positioning from your Pointr Cloud instance but you want to toggle it on runtime, you can do it by changing the runtime configuration of the SDK. You would need to do the following;

val configuration = PTRMutableConfiguration()
    configuration.positionManagerConfiguration.isBackgroundPositioningEnabled = true
    configuration.positionManagerConfiguration.isGpsTrackingEnabledInBackground = true
    Pointr.getPointr()?.configurationManager?.updateRuntimeConfiguration(configuration)

Note

this will only work during the current run. Pointr will default back to your configuration settings defined in the Pointr Cloud instance when its restarted.

If you do not want to use background positioning, you should remove the permission declaration from the android manifest. Google Play reviewers usually require that you explain your use case for needing Background Positioning when submitting your app to Play Store. So if you do not require it, it is best to remove it.

Use the code below in your AndroidManifest.xml file, inside the scope <manifest>, before <application>:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" tools:node="remove" />

Enabling Background Positioning from Pointr Cloud

It is possible to enable background positioning using configuration parameters set on Pointr Cloud. For details on how to use these parameters please check this guide and for a more complete list of parameters check this informational article.

On the client configuration screen of Pointr Cloud, these are the parameters that are related to enabling background positioning:

  • positionManagerConfiguration_isBackgroundPositioningEnabled: Boolean, set to true to enable position calculations when the application is on the background.
  • positionManagerConfiguration_isGpsTrackingEnabledInBackground: Boolean, set to true to enable the usage of GPS (for example when outdoors on a parking lot) when the application is running on the background.
  • geofenceManagerConfiguration_betweenScanPeriodBackground (Android specific): Integer, miliseconds, how much time to wait before turning on sensors again when application is on background, lowering this value increases battery usage. Set the value to 0 (zero) if your app is expected to behave like in foreground while in background.
  • geofenceManagerConfiguration_scanPeriodBackground (Android specific): Integer, miliseconds, how much time to use the sensors before turning them off when application is on background, increasing this value increases battery usage. Set the value to 1100 if your app is expected to behave like in foreground while in background.

Note

For Android 7+, system does not allow stopping and starting BLE scans more than 5 times in a window of 30 seconds.

How to decrease battery consumption while using the App on the background?

Reducing GPS accuracy will help to decrease battery consumption while using the App on the background. It is possible to configure GPS accuracy on Pointr Cloud Dashboard changing the value of positionManagerConfiguration_gpsAccuracyModeInBackground parameter. Increasing this value will decrease the accuracy and battery consumption.

Please follow the steps below to decrease battery consumption:

  1. Go to Pointr Cloud Dashboard and click on “Edit” button,

  2. Click on “Advanced Configuration” button,

  3. Increase the value. Maximum value is 7 for this parameter.

    Warning

    On iOS, the GPS accuracy impacts the system’s capability of listening to beacons in the background and therefore getting indoor position while in the background. In order to guarantee that beacons are detected in the background, the GPS accuracy needs to be set between 1 and 4.

  4. Click on the “Confirm” button and publish the changes.

Note

Changing the settings below might break positioning on Android. Please do not make any changes on them.

  • geofenceManagerConfiguration_betweenScanPeriodBackground (Android specific)
  • geofenceManagerConfiguration_scanPeriodBackground (Android specific)

What gpsAccuracyMode values mean?

Android

  • [BEST_FOR_NAVIGATION = 1] to [com.google.android.gms.location.LocationRequest.PRIORITY_HIGH_ACCURACY]
  • [BEST = 2] to [com.google.android.gms.location.LocationRequest.PRIORITY_HIGH_ACCURACY]
  • [TEN_METERS = 3] to [com.google.android.gms.location.LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY]
  • [HUNDRED_METERS = 4] to [com.google.android.gms.location.LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY]
  • [KM = 5] to [com.google.android.gms.location.LocationRequest.PRIORITY_LOW_POWER]
  • [THREE_KM = 6] to [com.google.android.gms.location.LocationRequest.PRIORITY_LOW_POWER]
  • [SIGNIFICANT_ONLY = 7] to [com.google.android.gms.location.LocationRequest.PRIORITY_LOW_POWER]

IOS

  • Normal Gps Services runs for 1-6
  • Significant Location Services runs for 7 and 0

Understanding the Impact of App Backgrounding on Pointr’s Positioning Engine

Pointr’s positioning engine uses a combination of GPS, BLE, motion sensors (Gyroscope, Accelerometer) and Barometer for its high accuracy output. However, both iOS and Android platforms restrict access to sensor data when an app is running in the background.

If background positioning is DISABLED:

  • Heading is lost - no motion sensor data
  • Location is lost - no beacon or GPS data

If background positioning is ENABLED:

  • Heading is lost - no motion sensor data
  • Location is available - through beacon or GPS data

When in background, if background positioning is enabled by configuration and permitted by the user, Pointr can produce a lower accuracy output using GPS and BLE only. This also allows for reduced battery usage.


Last update: October 10, 2024
Back to top