Table of Contents
- Installation
- Quick Start
- Initialization Lifecycle
- Configuration
- Core Features
- SDK Status
- Statistics API
- Best Practices
- API Reference
Installation
Add the following to your root-levelbuild.gradle file (or settings.gradle):
build.gradle:
Google Play foreground-service declaration (SDK 1.5.7+): the SDK’s recording service declares the
location and dataSync foreground-service types (previously location + specialUse). In the
Play Console foreground-service-types form, declare “Location” and “Data sync” for the Truemetrics
recording service (and the optional sensor-watchdog service, if enabled). If your existing declaration
listed “Special use” for a Truemetrics service, replace it with “Data sync” (keep “Location”).Quick Start
1. Initialize the SDK in your Application class
2. Start and stop recordings
Initialization Lifecycle
TruemetricsSdk.init() returns immediately but the SDK is not ready for use until it reaches Status.Initialized. Initialization runs asynchronously in the background: it binds a foreground service, then fetches the configuration from the backend.
State Transitions
How Long To Wait
There is no hard timeout on initialization. Under poor connectivity the SDK stays in
Status.Initializing and keeps retrying. Do not impose an arbitrary client-side timeout — either wait for Status.Initialized/Status.Error, or gate UI on getDeviceId() becoming non-null.Checking Initialization Status
Recommended — observe the status flow and wait forStatus.Initialized:
getDeviceId() returns null until initialization completes:
Calling Other APIs Before Initialization Completes
startRecording()— safe to call immediately afterinit(). If the service is not yet bound, the request is queued and executed once binding completes. However, if called after the service binds but before the config fetch succeeds, it will transition the SDK toStatus.Error(CONFIG_ERROR). To avoid this, either awaitStatus.Initializedfirst, or rely on the auto-start configuration (see Configuration).logMetadata()— ignored with a log warning if called before initialization. Wait forStatus.Initializedbefore logging.getDeviceId(),getActiveConfig(),getUploadStatistics(),getSensorStatistics()— returnnulluntil initialization completes.
Terminal vs. Transient Failures
Configuration
The SDK is configured using theSdkConfiguration.Builder class.
Basic Configuration
Custom Foreground Notification
Customize the notification shown when the SDK is running as a foreground service using the SDK’s notification channel:Core Features
Recording Management
Device ID
Get the unique device identifier (available after initialization):The device ID may rotate automatically based on a server-configured TTL. When expired, a new ID is generated transparently on the next initialization.
Metadata Logging
Log standardized delivery/pickup event metadata:logMetadata(Map<String, String>) overload is deprecated. Use logMetadata(StandardMetadata) instead.
For advanced metadata with templates and tags, see Metadata Guide.
Sensor Management
Complete Cleanup
deinitialize(), the SDK instance becomes unusable. You must call
init() again to use the SDK.
SDK Status
Monitor the SDK state usingobserveSdkStatus():
Error Codes
Statistics API
Upload Statistics
Monitor upload health:Sensor Statistics
Get detailed sensor data quality:- EXCELLENT: 95-100% of configured frequency
- GOOD: 80-95%
- POOR: 50-80%
- BAD: less than 50%
- UNKNOWN: no data or not recording
Best Practices
1. Initialization
- Always initialize in your
Applicationclass - Use the application context, not activity context
- Initialize once and reuse the singleton instance