pinpoint_sdk 12.2.0-delta
pinpoint_sdk: ^12.2.0-delta copied to clipboard
The Pinpoint SDK is a cross-platform (Android/iOS) Flutter plugin for positioning with Pinpoint's technology.
Pinpoint SDK #
Introduction #
The Pinpoint SDK is a cross-platform (Android/iOS) Flutter plugin for Ultra-Wideband (UWB) positioning with Pinpoint's technology.
Note: this a prerelease and is only meant for wrapping the functions of the geolocator package. It does not yet include any Pinpoint functionality.
This plugin in based on version 13.0.3 of geolocator.
The integration of navigation based on Pinpoint's technology will be included in the next release.
Features #
- Get the last known location;
- Get the current location of the device
- Get continuous location updates
- Check if location services are enabled on the device
- Calculate the distance (in meters) between two geocoordinates
- Calculate the bearing between two geocoordinates
Getting started #
Requirements #
- Flutter (minimum version: 3.27.0)
- Visual Studio Code (minimum version: 1.92.2) or Android Studio (minimum version: Android Studio Giraffe | 2022.3.1)
Supported Platforms #
- Android (minimum Android 10, API level 29)
- iOS (minimum 11)
Setup Environment #
To add the pinpoint_sdk to your Flutter application read the install instructions. Below are some Android and iOS specifics that are required for the plugin to work correctly.
Android
AndroidX
The pinppoint_sdk plugin requires the AndroidX version of the Android Support Libraries. This means you need to make sure your Android project supports AndroidX. Detailed instructions can be found here.
The TL;DR version is:
- Add the following to your "gradle.properties" file:
android.useAndroidX=true
android.enableJetifier=true
- Make sure you set the
compileSdkVersionin your "android/app/build.gradle" file to 35:
in android/app/build.gradle:
android {
compileSdkVersion 35
}
or if applicable in android/app/build.gradle.kts
android {
compileSdk = 35
}
- Make sure you replace all the
android.dependencies to their AndroidX counterparts (a full list can be found here: Migrating to AndroidX).
Minimum SDK
Make sure to set the minimum supported Android version to Android 10 (API level 29):
in android/app/build.gradle:
defaultConfig {
minSdkVersion 29
}
or if applicable in android/app/build.gradle.kts:
defaultConfig {
minSdk = 29
}
Proguard
Add the following lines to your project/android/app/proguard-rules.pro file:
-keep class com.lib.flutter_blue_plus.* { *; }
-keep public class de.IPinpointPositionListener$Stub.** { *; }
-keep public class de.IPinpointPositionListener.** { *; }
-keep public class de.pinpoint.ServiceTest.** { *; }
-ignorewarnings
If the file does not exist yet, create it. Also make sure that your proguard file is used:
in android/app/build.gradle
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(
'proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
}
or if applicable in android/app/build.gradle.kts:
android {
buildTypes{
release {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}
Permissions
On Android you'll need to add the ACCESS_FINE_LOCATION permission to your Android Manifest. Open the AndroidManifest.xml file (located under android/app/src/main) and add the following line as direct child of the <manifest> tag:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Starting from Android 10 you need to add the ACCESS_BACKGROUND_LOCATION permission (next to the ACCESS_FINE_LOCATION permission) if you want to continue receiving updates even when your App is running in the background:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
WARNING: You cannot use the permission
ACCESS_COARSE_LOCATIONdue to compatibility reasons with the Pinpoint SDK. If you use it, this might cause the app to crash.
iOS
On iOS you'll need to add the following entry to your Info.plist file (located under ios/Runner) in order to access the device's location. Simply open your Info.plist file and add the following (make sure you update the description so it is meaningful in the context of your App):
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
If you don't need to receive updates when your app is in the background, then add a compiler flag as follows: in XCode, click on Pods, choose the Target 'geolocator_apple', choose Build Settings, in the search box look for 'Preprocessor Macros' then add the BYPASS_PERMISSION_LOCATION_ALWAYS=1 flag.
Setting this flag prevents your app from requiring the NSLocationAlwaysAndWhenInUseUsageDescription entry in Info.plist, and avoids questions from Apple when submitting your app.
You can also have the flag set automatically by adding the following to the ios/Podfile of your application:
post_install do |installer|
installer.pods_project.targets.each do |target|
if target.name == "geolocator_apple"
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)', 'BYPASS_PERMISSION_LOCATION_ALWAYS=1']
end
end
end
end
If you do want to receive updates when your App is in the background (or if you don't bypass the permission request as described above) then you'll need to:
- Add the Background Modes capability to your XCode project (Project > Signing and Capabilities > "+ Capability" button) and select Location Updates. Be careful with this, you will need to explain in detail to Apple why your App needs this when submitting your App to the AppStore. If Apple isn't satisfied with the explanation your App will be rejected.
- Add an
NSLocationAlwaysAndWhenInUseUsageDescriptionentry to your Info.plist (useNSLocationAlwaysUsageDescriptionif you're targeting iOS <11.0)
When using the requestTemporaryFullAccuracy({purposeKey: "YourPurposeKey"}) method, a dictionary should be added to the Info.plist file.
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<dict>
<key>YourPurposeKey</key>
<string>The example App requires temporary access to the device's precise location.</string>
</dict>
The second key (in this example called YourPurposeKey) should match the purposeKey that is passed in the requestTemporaryFullAccuracy() method. It is possible to define multiple keys for different features in your app. More information can be found in Apple's documentation.
NOTE: the first time requesting temporary full accuracy access it might take several seconds for the pop-up to show. This is due to the fact that iOS is determining the exact user location which may take several seconds. Unfortunately this is out of our hands.
Usage #
- Install the Pinpoint SDK by following the instructions from Getting started. Pay attention to the platform specific requirements (Android, iOS).
- Import it:
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
- Call the function
initGeolocatorto enable the wrapping of geolocator. This function only needs to be called once at a central place, e.g. in the main of your app:
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
void main () {
// wraps original GeolocatorPlatform instance
initGeolocator();
runApp(const GeolocatorApp());
}
Migration from geolocator #
-
Follow the steps from above (Usage). Pay attention that under Android you can only use the permission
ACCESS_FINE_LOCATION.ACCESS_COARSE_LOCATIONis not supported. -
Remove
geolocatorfrom the dependencies of your app by deleting it from the dependencies in your pubspec.yaml or by callingflutter pub remove geolocator -
Replace the original imports of
package:geolocator/geolocator.dartwithpackage:pinpoint_sdk/pinpoint_sdk.dart
Example #
The code below shows an example on how to acquire the current position of the device, including checking if the location services are enabled and checking / requesting permission to access the position of the device:
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
/// Determine the current position of the device.
///
/// When the location services are not enabled or permissions
/// are denied the `Future` will return an error.
Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled don't continue
// accessing the position and request users of the
// App to enable the location services.
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
// When we reach here, permissions are granted and we can
// continue accessing the position of the device.
return await Geolocator.getCurrentPosition();
}
API #
Geolocation #
Current location
To query the current location of the device simply make a call to the getCurrentPosition method. You can finetune the results by specifying the following parameters:
desiredAccuracy: the accuracy of the location data that your app wants to receive;timeLimit: the maximum amount of time allowed to acquire the current location. When the time limit is passed aTimeoutExceptionwill be thrown and the call will be cancelled. By default no limit is configured.
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
Position position = await Geolocator.getCurrentPosition(locationSettings: locationSettings);
Last known location
To query the last known location retrieved stored on the device you can use the getLastKnownPosition method (note that this can result in a null value when no location details are available):
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
Position? position = await Geolocator.getLastKnownPosition();
Listen to location updates
To listen for location changes you can call the getPositionStream to receive stream you can listen to and receive position updates. You can finetune the results by specifying the following parameters:
accuracy: the accuracy of the location data that your app wants to receive;distanceFilter: the minimum distance (measured in meters) a device must move horizontally before an update event is generated;timeLimit: the maximum amount of time allowed between location updates. When the time limit is passed aTimeoutExceptionwill be thrown and the stream will be cancelled. By default no limit is configured.
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
final LocationSettings locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
(Position? position) {
print(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}');
});
Platform specific location settings #
In certain situation it is necessary to specify some platform specific settings. This can be accomplished using the platform specific AndroidSettings or AppleSettings classes. When using a platform specific class, the platform specific package of geolocator (geolocator_android for Android or geolocator_apple for iOS) must be imported as well. For example:
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
import 'package:geolocator_android/geolocator_android.dart';
import 'package:geolocator_apple/geolocator_apple.dart';
late LocationSettings locationSettings;
if (defaultTargetPlatform == TargetPlatform.android) {
locationSettings = AndroidSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
forceLocationManager: true,
intervalDuration: const Duration(seconds: 10),
//(Optional) Set foreground notification config to keep the app alive
//when going to the background
foregroundNotificationConfig: const ForegroundNotificationConfig(
notificationText:
"Example app will continue to receive your location even when you aren't using it",
notificationTitle: "Running in Background",
enableWakeLock: true,
)
);
} else if (defaultTargetPlatform == TargetPlatform.iOS) {
locationSettings = AppleSettings(
accuracy: LocationAccuracy.high,
activityType: ActivityType.fitness,
distanceFilter: 100,
pauseLocationUpdatesAutomatically: true,
// Only set to true if our app will be started up in the background.
showBackgroundLocationIndicator: false,
);
} else {
locationSettings = LocationSettings(
accuracy: LocationAccuracy.high,
distanceFilter: 100,
);
}
// supply location settings to getCurrentPosition
Position position = await Geolocator.getCurrentPosition(locationSettings: locationSettings);
// supply location settings to getPositionStream
StreamSubscription<Position> positionStream = Geolocator.getPositionStream(locationSettings: locationSettings).listen(
(Position? position) {
print(position == null ? 'Unknown' : '${position.latitude.toString()}, ${position.longitude.toString()}');
});
Location accuracy (Android and iOS 14+ only)
To query if a user enabled Approximate location fetching or Precise location fetching, you can call the Geolocator().getLocationAccuracy() method. This will return a Future<LocationAccuracyStatus>, which when completed contains a LocationAccuracyStatus.reduced if the user has enabled Approximate location fetching or LocationAccuracyStatus.precise if the user has enabled Precise location fetching.
When calling getLocationAccuracy before the user has given permission, the method will return LocationAccuracyStatus.reduced by default.
On iOS 13 or below, the method getLocationAccuracy will always return LocationAccuracyStatus.precise, since that is the default value for iOS 13 and below.
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
var accuracy = await Geolocator.getLocationAccuracy();
Location service information
To check if location services are enabled you can call the isLocationServiceEnabled method:
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
bool isLocationServiceEnabled = await Geolocator.isLocationServiceEnabled();
To listen for service status changes you can call the getServiceStatusStream. This will return a Stream<ServiceStatus> which can be listened to, to receive location service status updates.
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
StreamSubscription<ServiceStatus> serviceStatusStream = Geolocator.getServiceStatusStream().listen(
(ServiceStatus status) {
print(status);
});
Permissions #
If you want to check if the user already granted permissions to acquire the device's location you can make a call to the checkPermission method:
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
LocationPermission permission = await Geolocator.checkPermission();
If you want to request permission to access the device's location you can call the requestPermission method:
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
LocationPermission permission = await Geolocator.requestPermission();
Possible results from the checkPermission and requestPermission methods are:
| Permission | Description |
|---|---|
| denied | Permission to access the device's location is denied by the user. You are free to request permission again (this is also the initial permission state). |
| deniedForever | Permission to access the device's location is permanently denied. When requesting permissions the permission dialog will not be shown until the user updates the permission in the App settings. |
| whileInUse | Permission to access the device's location is allowed only while the App is in use. |
| always | Permission to access the device's location is allowed even when the App is running in the background. |
Note: Android can only return
whileInUse,alwaysordeniedwhen checking permissions. Due to limitations on the Android OS it is not possible to determine if permissions are denied permanently when checking permissions. Using a workaround the geolocator is only able to do so as a result of therequestPermissionmethod. More information can be found in geolocator's wiki.
Settings #
In some cases it is necessary to ask the user and update their device settings. For example when the user initially permanently denied permissions to access the device's location or if the location services are not enabled (and, on Android, automatic resolution didn't work). In these cases you can use the openAppSettings or openLocationSettings methods to immediately redirect the user to the device's settings page.
On Android the openAppSettings method will redirect the user to the App specific settings where the user can update necessary permissions. The openLocationSettings method will redirect the user to the location settings where the user can enable/ disable the location services.
On iOS we are not allowed to open specific setting pages so both methods will redirect the user to the Settings App from where the user can navigate to the correct settings category to update permissions or enable/ disable the location services.
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
await Geolocator.openAppSettings();
await Geolocator.openLocationSettings();
Utility methods #
To calculate the distance (in meters) between two geocoordinates you can use the distanceBetween method. The distanceBetween method takes four parameters:
| Parameter | Type | Description |
|---|---|---|
| startLatitude | double | Latitude of the start position |
| startLongitude | double | Longitude of the start position |
| endLatitude | double | Latitude of the destination position |
| endLongitude | double | Longitude of the destination position |
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
double distanceInMeters = Geolocator.distanceBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);
If you want to calculate the bearing between two geocoordinates you can use the bearingBetween method. The bearingBetween method also takes four parameters:
| Parameter | Type | Description |
|---|---|---|
| startLatitude | double | Latitude of the start position |
| startLongitude | double | Longitude of the start position |
| endLatitude | double | Latitude of the destination position |
| endLongitude | double | Longitude of the destination position |
import 'package:pinpoint_sdk/pinpoint_sdk.dart';
// call init function for wrapping of Geolocator (only needs to be done once at a central place, e.g. in main of app)
initGeolocator();
double bearing = Geolocator.bearingBetween(52.2165157, 6.9437819, 52.3546274, 4.8285838);
Location accuracy #
Android
On Android, the LocationAccuracy enum controls the accuracy of the location data the app wants to receive. It also provides control over the priority given to the location stream. This can be confusing, as a priority of lowest might not return any location, while one might expect it to give the quickest responses. The table below outlines the priority and its meaning per accuracy option:
| Location accuracy | Android priority | Description |
|---|---|---|
| lowest | PRIORITY_PASSIVE | Ensures that no extra power will be used to derive locations. This enforces that the request will act as a passive listener that will only receive "free" locations calculated on behalf of other clients, and no locations will be calculated on behalf of only this request. |
| low | PRIORITY_LOW_POWER | Requests a tradeoff that favors low power usage at the possible expense of location accuracy. |
| medium | PRIORITY_BALANCED_POWER_ACCURACY | Requests a tradeoff that is balanced between location accuracy and power usage. |
| high+ | PRIORITY_HIGH_ACCURACY | Requests a tradeoff that favors highly accurate locations at the possible expense of additional power usage. |
iOS
On iOS, the LocationAccuracy enum controls the accuracy of the location data the app wants to receive. It also provides control on the battery consumption of the device: the more detailed data is requested, the larger the impact on the battery consumption. More details can be found on Apple's documentation. The table below shows how the LocationAccuracy values map to the native iOS accuracy settings.
| Location accuracy | iOS accuracy | Description |
|---|---|---|
| lowest | kCLLocationAccuracyThreeKilometers | Accurate to the nearest three kilometers. |
| low | kCLLocationAccuracyKilometer | Accurate to the nearest kilometer. |
| medium | kCLLocationAccuracyHundredMeters | Accurate to within one hundred meters. |
| high | kCLLocationAccuracyNearestTenMeters | Accurate to within ten meters of the desired target. |
| best | kCLLocationAccuracyBest | The best level of accuracy available. |
| bestForNavigation | kCLLocationAccuracyBestForNavigation | The highest possible accuracy that uses additional sensor data to facilitate navigation apps. |
License #
This package is licensed under a proprietary license with MIT licensed components. Please refer to the LICENSE file for more details.