SpikeSDK Android (v3)
Current Android(Kotlin) SDK Version: 4.2.32
Android Package Package can be found here (api reference)
Example app can be found here
Android 9.0+ (Level 28, P, Pie)
To add the SDK to your project, you have to add the following to your project's build.gradle file in the repositories block.
After that, add the following to your app's build.gradle file in the dependencies block.
Include the necessary health permissions in your AndroidManifest.xml to fully leverage the Spike SDK and access data from apps integrated with Health Connect. Please refer to this guide for details on the required permissions.
Note: Only request permissions essential to your app’s functionality. Requesting unused permissions may lead to Play Store rejections.
Add an intent filter to your activity definition so that you can request the permissions at runtime.
To handle Android 14 you also need to add activity-alias to your AndroidManifest.xml It is just a wrapper for the activity that requests permissions so no real activity is necessary.
To set up the Spike SDK create SpikeConnectionV3 instance with your Spike application id, auth token and user id unique to each of your users:
If you want to read data from Android Health Connect, you have to ensure user gives your app permissions. First, you have to check if Health Connect is available on users phone:
where
If update is required, you can use Spike helper to open Play Store for user to install Health Connect:
If Health Connect is installed, you can get permissions that are needed and a list of already provided permissions:
If you have missing permissions you can ask Android to present user with a modal asking user for permission to read the data. Example for Compose:
- Please note that users might only grant partial permissions. In such cases, it’s up to you to decide whether your app can function effectively with limited access. The SpikeSDK itself will still operate even without full permissions; however, it may result in no data being returned in certain scenarios. Conversely, if your app has been granted additional permissions beyond the minimum required for specific data types, we may enhance certain entries by incorporating data sourced from other types (e.g., identifying manually entered data).
You can now use StatisticsFilter(providers = listOf(Provider.HEALTH_CONNECT)) to specifically retrieve data from Health Connect. Alternatively, you can omit the providers parameter entirely and allow Spike to choose the most suitable data source based on your request.
Info: The maximum permitted date range is 90 days There are two types of data you can retrieve from Spike:
- Records consist of the raw data points collected from user devices or applications.
- Statistics, on the other hand, are calculated values derived from records.
Get daily statistics for steps and total distance from health Connect:
where:
Get all records we have from Garmin provider:
where
Get daily statistics for steps and total distance from Garmin:
where:
Get all records we have from Garmin provider:
where
Background delivery ensures that data updates are sent to your backend via webhooks, even when the application is in the background or closed.
- Background delivery is scheduled to run every hour. But ultimately Android decides when the delivery will be executed.
- Android may throttle the frequency of updates for background delivery depending on the app's activity, battery state, etc.
- Android may stop background delivery if it detects that the app is not active for a long time.
- There is a limit of queries that can be done in Health Connect and it is different for foreground and background reads, so please request only essential data to be delivered in background. More information on (Health Connect documentation)
Important: The SpikeSDK, along with any other applications, cannot guarantee data synchronization on a fixed schedule. The hourly sync interval serves as a guideline rather than a strict requirement enforced by Android. Consequently, the actual synchronization frequency may vary, occurring hourly, once per day, or during specific system-defined events, such as the conclusion of Sleep Mode or when the device begins charging.
Add the following permission to your AndroidManifest.xml:
Check if background delivery is possible with current Health Connect version:
If not, you can ask user to update Health Connect to the latest version.
Ask for background read permission:
You can also ask for background delivery permission in the same time as other permissions.
Now you can enable background delivery:
Keep in mind that calling enableBackgroundDeliverywill overwrite previous configuration. If you want to add more types, you have to call enableBackgroundDeliveryagain with updated configuration:
To check current configuration call getBackgroundDeliveryConfig()method.
To stop background delivery call disableBackgroundDelivery()method.
By default SPikeSDK logs are sent to standard Android Logcat. If for some reason you want to read them, you can use the following code (running it before you start using SpikeSDK):
If you are utilising Background Delivery, this code should be run in your application's onCreatemethod:
Also application should be set in AndroidManifest.xml: