Traccar Client
repository·main·Indexed 41 days ago
https://github.com/traccar/traccar-clientA mobile GPS tracking application for Android and iOS built with Flutter that sends real-time location data to a Traccar server. It features background operation, customizable update intervals, and support for programmatic configuration via URIs, maps, and deep links.
What's inside Traccar Client
- Traccar Client is a GPS tracking application for Android and iOS designed to send real-time location updates to a Traccar server. It operates in the background and allows for high customization of update intervals, accuracy, and data usage. The app is privacy-focused, sending data only to the server address you specify.
Build the Traccar Client project
mainThe project is a standard Flutter application. To build and run the project locally, use the following commands:
flutter pub get flutter runSet up Traccar Client for tracking
mainTo start tracking your device, follow these steps:
- Install the app on your Android or iOS device.
- Enter your Traccar server address in the app configuration.
- Grant the necessary location permissions.
- The app will then automatically send periodic location reports in the background.
Control Traccar Client via Deep Links
mainThe Traccar Client supports deep linking via URI schemes to perform specific actions or configure the application.
Action Links
Links with the host
actioncan trigger geolocation tracking directly:[scheme]://action/start: Starts the geolocation tracker.[scheme]://action/stop: Stops the geolocation tracker.
Note: These actions may fail with a
PlatformExceptionif permissions are denied or if there is a startup error.Configuration Links
Other URIs are treated as configuration updates. When a non-action URI is received:
- An alert dialog is shown to the user with a configuration message.
- If the user confirms,
ConfigurationService.applyUri(uri)is called to apply the new settings. - The
MainScreenis refreshed to reflect changes.
// Example Action URIs: [scheme]://action/start [scheme]://action/stop // Example Configuration URI: [scheme]://your-server.com/config-paramsInitialize Traccar Client services
mainThe application entrypoint performs a sequence of asynchronous initializations required for the client to function correctly. If you are integrating or extending the startup logic, ensure the following services are initialized in order:
Firebase.initializeApp(): Initializes Firebase for crash reporting.Preferences.init(): Loads local user preferences.GeolocationService.tracker.init(Preferences.buildConfig()): Initializes the GPS tracking engine using the loaded configuration.PasswordService.migrate(): Handles migration of stored credentials.PushService.init(): Sets up push notification handling.ManagedConfigService.init(): Initializes managed configuration settings.
void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); // ... error handling ... await Preferences.init(); await GeolocationService.tracker.init(Preferences.buildConfig()); await PasswordService.migrate(); await PushService.init(); await ManagedConfigService.init(); runApp(const MainApp()); }Build a Config object from local preferences
mainOnce initialized, use
Preferences.buildConfig()to generate aConfigobject. This object is populated with the current values stored in the device's local storage, including server URL, device ID, and location settings. This is the primary way to retrieve the application's current operational state.await Preferences.init(); final config = Preferences.buildConfig(); print(config.serverUrl);Initialize the Preferences service
mainBefore accessing or building configurations, you must initialize the
Preferencesservice. This ensures the underlyingSharedPreferencesWithCacheinstance is created and the local storage is ready. CallPreferences.init()and await its completion.await Preferences.init();Configure the Traccar Client via URI or Map
mainThe
ConfigurationServiceallows you to programmatically configure the client's settings (such as server URL, reporting interval, and accuracy) using either aUrior aMap<String, dynamic>.- Using a URI: Use
applyUri(Uri uri)to parse configuration from a URI. If the scheme ishttporhttps, the origin and path are used as the server URL. Otherwise, it looks for aurlquery parameter. - Using a Map: Use
applyManagedConfig(Map<String, dynamic> config)orapplyParameters(Map<String, dynamic> parameters, {String? url})to apply settings directly from a configuration map.
// Example: Applying configuration from a Map await ConfigurationService.applyParameters({ 'url': 'https://your-traccar-server.com', 'id': 'device_id_123', 'interval': 60, 'accuracy': 'high', }); // Example: Applying configuration from a URI await ConfigurationService.applyUri(Uri.parse('https://your-traccar-server.com/config?id=device_id_123&interval=60'));- Using a URI: Use
Reference: Configuration Parameter Keys
mainWhen passing a configuration map to
ConfigurationService.applyParameters, use the following keys to set specific client behaviors. Note that types must match the expected format (String, int, or bool).### String Parameters - `id`: The unique identifier for the device. - `accuracy`: The GPS accuracy setting. - `url`: The server address (can be passed separately or within the map). ### Integer Parameters - `distance`: Distance threshold for updates. - `interval`: Time interval between updates. - `angle`: Angle threshold for updates. - `heartbeat`: Heartbeat interval. ### Boolean Parameters - `buffer`: Whether to buffer data. - `wakelock`: Whether to use a wakelock to keep the device active. - `stopDetection`: Whether to stop detection. - `preferPlatformProviders`: Whether to prefer platform-specific location providers.Reference: Preference keys
mainThe following keys are used by the
Preferencesclass to store and retrieve configuration data in local storage:static const String id = 'id'; static const String url = 'url'; static const String accuracy = 'accuracy'; static const String distance = 'distance'; static const String interval = 'interval'; static const String angle = 'angle'; static const String heartbeat = 'heartbeat'; static const String buffer = 'buffer'; static const String wakelock = 'wakelock'; static const String stopDetection = 'stop_detection'; static const String preferPlatformProviders = 'prefer_platform_providers'; static const String password = 'password';