Mobile SDK

Flutter

Current Flutter SDK Version: 1.2.9

Flutter Package can be found here



Spike Flutter SDK is a library on top of Apple HealthKit and Android Health Connect that helps with the extraction of data, and pushes data to SpikeAPI and delivers standardized data.

Features

  1. Read Apple HealthKit and Android Health Connect data.
  2. Extract processed data to preferred backend.
  3. Register periodic background tasks to send and read the data for iOS devices.
  4. Track various events of the package by utilizing shared preferences.


🔔 SDK Version Update Guide - iOS Considerations

Always Update Dependencies:

After changing the Flutter SDK version, execute the pod update command from the Flutter's iOS folder. This ensures all dependencies are up-to-date with the new SDK version.



Getting Started

iOS Requirements

  • iOS 13.0+
  • Xcode 13.0+
  • Health Kit Reporter 2.1.0+
  • Background fetch 1.0.3+

Android Requirements

  • Android 9.0+


iOS Setup Guide

Follow Flutter HealthKit setup instructions here

If background tasks are of interest, configure your Flutter app according to the package guide here

Signing & Capabilities

On iOS, go to your target's Signing & Capabilities section and add HealthKit. Allow Background fetch and Background Delivery.

Document image


On iOS, add HealthKit permissions descriptions to your Info.plist file.

info.plist


Android Setup Guide

Important: For Android 14, carefully follow the Android 14 Setup Guide for additional instructions.

General Setup for Android 9 to 13

Add the required health permissions to a resource file. You can reuse an existing one or create a new one. That allows you to fully utilise this SDK and to read the necessary data from apps that integrate with HealthConnect.

  • Create config.xml in the following path: android/app/src/main/res/values. Content of this file must be the following:
config.xml


You have to add the required health permissions to your AndroidManifest.xml to fully utilize Spike SDK and to read the necessary data from apps that integrate with HealthConnect. Please refer to this guide to understand the specific permissions needed.

  • Now modify the AndroidManifest.xml file. Add the following query:
AndroidManifest

  • Reference the permissions to your AndroidManifest.xml file in the activity that will call for health permissions. This meta-data reference should be added under the following:
AndroidManifest.xml

  • Add intent filter to your activity definition so that you can request the permissions at runtime. This intent filter reference should be added under the following:
AndroidManifest.xml


If permissions handling is not working, this might be related to launch mode being singleTop. This might be not needed, but some apps face problems when requesting permissions. If you face them, then you should try removing the following line:

android:launchMode="singleTop"

If app is not building, it might be related to label replacement issue. In this case, you should add the following line:

tools:replace="android:label"
  • Add the following line into the android/app/build.gradle under dependencies:
build.gradle

  • Since we are using closed source native Android SDK, separate repository is needed. Thus, add the following dependency into your android/builde.gradle file (it must be added both in repositories and allprojects node of repositories):
build.gradle

  • In local.properties of your project for Flutter, add these lines:
local.properties

  • In case your default Flutter project does not take these values, do the necessary changes in the android/app/build.gradle
build.gradle

  • Add or change compileSdkVersion under android node like this:
Android Node


Need Help? Encounter an issue not covered here? Contact us with detailed information for support. Different environments can cause unique problems, and we're here to help.

Android 14 Setup Guide

  • Make sure to follow General Setup for Android 9 to 13, and it works completely with devices before Android 14. Otherwise, these steps might not work.
  • Check the contents of android/app/src/main/kotlin/{YOUR_PACKAGE_ID}/MainActivity.kt. You must see something like that in case it is the new app being developed:
MainActivity

  • You must change FlutterActivity to the FlutterFragmentActivity which means that you code should turn into the result similar to the one below:
MainActivity

  • In the Health Connect permissions activity, there is a link to your privacy policy. You need to grant the Health Connect app access in order to link back to your privacy policy. In the example below, you should either replace .MainActivity with an activity that presents the privacy policy or have the Main Activity route the user to the policy. This step may be required to pass Google app review when requesting access to sensitive permissions.
<activity-alias android:name="ViewPermissionUsageActivity" android:exported="true" android:targetActivity=".MainActivity" android:permission="android.permission.START_VIEW_PERMISSION_USAGE"> <intent-filter> <action android:name="android.intent.action.VIEW_PERMISSION_USAGE" /> <category android:name="android.intent.category.HEALTH_PERMISSIONS" /> </intent-filter> </activity-alias>
  • Android 14 uses different model for the Health permissions. So you must add the following permissions for requesting into the AndroidManifest.xml:
XML



Spike SDK Usage

To set up the Flutter SDK, provide the application ID, authentication token, and your customer user ID. You can find the application ID and authentication token in the Spike Developer Console. Personal identifiable information, such as emails, should not be part of user IDs.

Initialization

const authToken = '{YOUR_AUTH_TOKEN}'; const appId = '{YOUR_APP_ID}'; const customerEndUserId = '{YOUR_CUSTOMER_END_USER_ID}'; const postbackURL = 'https://example.com/';

Requesting Permissions

In order to use the connection properly, you must first ask for the permissions needed for your purposes. Let us assume you want to get heart data.

connection.ensurePermissionsAreGranted(types: [SpikeDataType.heart]);

Creating Spike Connection

Using the credentials received, you can create the connection.

Dart


You can add a logger that logs the most important events in the connection. First, you would need to implement the SpikeLogger abstract class. Example of how to do so is below.

Dart


Getting and using the Data

Without specified Data Range

Dart


With specified Data Range

Dart


Getting Spike End User ID

Dart


Background Delivery

Background Deliveries are for WebHook connection and iOS only. To enable background deliveries:

Dart


Listener for Background Deliveries

You can implement SpikeWebhookConnectionListener, and provide your implementation as listener for the background delivery. Below, there is an example of how to implement SpikeWebhookConnectionListener.

Dart


Checking Background Delivery Types

Dart



Publishing to Play Store

The SpikeSDK for Android devices integrates with Health Connect for data retrieval. Before publishing the application to the Play Store, application owners are required to complete the Developer Declaration Form available at the following link: Developer Declaration Form

Data types

The following table shows the data types that can be read from HealthConnect API and the permissions that are required to read them.

Spike Data Type

HealthConnect permission

SpikeDataTypes.HEART_RATE

android.permission.health.READ_HEART_RATE

android.permission.health.READ_HEART_RATE_VARIABILITY

android.permission.health.READ_RESTING_HEART_RATE

SpikeDataTypes.DISTANCE

android.permission.health.READ_DISTANCE

SpikeDataTypes.ACTIVITIES_STREAM

android.permission.health.READ_EXERCISE

android.permission.health.READ_STEPS

android.permission.health.READ_DISTANCE

android.permission.health.READ_TOTAL_CALORIES_BURNED

android.permission.health.READ_HEART_RATE

android.permission.health.READ_SPEED

android.permission.health.READ_ELEVATION_GAINED

android.permission.health.READ_POWER

android.permission.health.READ_HEART_RATE_VARIABILITY

SpikeDataTypes.ACTIVITIES_SUMMARY

android.permission.health.READ_STEPS

android.permission.health.READ_ACTIVE_CALORIES_BURNED

android.permission.health.READ_TOTAL_CALORIES_BURNED

android.permission.health.READ_DISTANCE

android.permission.health.READ_ELEVATION_GAINED

android.permission.health.READ_FLOORS_CLIMBED

android.permission.health.READ_HEART_RATE

android.permission.health.READ_RESTING_HEART_RATE

android.permission.health.READ_BASAL_METABOLIC_RATE

SpikeDataTypes.BREATHING

android.permission.health.READ_RESPIRATORY_RATE

SpikeDataTypes.CALORIES

android.permission.health.READ_TOTAL_CALORIES_BURNED

android.permission.health.READ_ACTIVE_CALORIES_BURNED

SpikeDataTypes.GLUCOSE

android.permission.health.READ_BLOOD_GLUCOSE

SpikeDataTypes.OXYGEN_SATURATION

android.permission.health.READ_OXYGEN_SATURATION

SpikeDataTypes.SLEEP

android.permission.health.READ_SLEEP

android.permission.health.READ_HEART_RATE

android.permission.health.READ_HEART_RATE_VARIABILITY

SpikeDataTypes.STEPS

android.permission.health.READ_STEPS

SpikeDataTypes.BODY

android.permission.health.READ_WEIGHT

android.permission.health.READ_HEIGHT

android.permission.health.READ_BODY_FAT

android.permission.health.READ_BONE_MASS

android.permission.health.READ_BLOOD_PRESSURE

android.permission.health.READ_BODY_TEMPERATURE

Classes

SpikeSDK

Class

Method

Description

Spike

createConnection

Creates a new SpikeConnection instance with the given user details.

Parameters: appId, authToken, customerEndUserId, logger

Returns: An instance of the SpikeConnection class.

Spike

getBackgroundConnections (iOS)

Returns all connections that are configured to deliver data in the background.

Returns: A collection (a set) of SpikeWebhookConnection instances.

Spike

isPackageInstalled (Android)

Check if Health Connect is installed on a device.

SpikeConnection

Class

Method

Description

iOS

Android

SpikeConnection

getAppId

Retrieves the unique Spike application identifier.

Returns: string

Yes

Yes

SpikeConnection

getSpikeEndUserId

Retrieves the unique identifier assigned to the end-user by Spike.

Returns: string

Yes

Yes

SpikeConnection

getCustomerEndUserId

Retrieves the unique identifier assigned to the end-user by the customer.

Returns: string

Yes

Yes

SpikeConnection

close

Terminates any ongoing connections with Spike’s backend servers, clears any caches, and removes provided user details and tokens from the memory. Once the connection is closed, it cannot be used, and any method other than close() will throw a SpikeConnectionIsClosed exception.

Yes

Yes

SpikeConnection

extractData

Extracts local device data for the current date in the end-user’s time zone.

Parameters: dataType(SpikeDataType)

Returns: An instance of SpikeData. The concrete type will depend on the data type requested.

Yes

Yes

SpikeConnection

extractAndPostData

Extracts local device data for the current date in the local user time zone and sends it as a webhook notification to the customer’s backend. Optionally time range can be provided.

*On iOS, the maximum allowed single-query time interval is 90 days. If required, data of any longer time period can be accessed by iterating multiple queries of 90 days.

*On Android the earliest date data can be requested is 30 days before permission was given to the user's app to read from HealthConnect. There is no limit on how many days in total. Parameters: config (SpikeExtractConfig)

Yes

Yes

SpikeConnection

manageHealthConnect

Opens your device's HealthConnect menu, where you can switch on and off data access for the app as well as delete data.

No

Yes

SpikeConnection

checkPermissionsGranted

Initiates a check on whether all health permissions have been granted to ensure proper functioning of the SDK

No

Yes

SpikeConnection

getHealthConnectAvailability

Provides information on whether HealthConnect is available on the device.

No

Yes

SpikeConnection

revokeAllPermissions

Revokes all granted health permissions for the application.

No

Yes

SpikeConnection

requestHealthPermissions

Requests all the supported health read permissions from HealthConnect.

No

Yes

Errors and Exceptions

SpikeException

The abstract parent of all concrete exceptions.

SpikeConnectionIsClosedException

Thrown if any method is invoked on SpikeConnection or one of its subclasses after the connection has been closed.

SpikeInvalidCredentialsException

Thrown if the credentials provided are not recognised, i.e. are invalid or expired.

SpikeInvalidDateRangeException

Thrown if duration exceeds the allowed maximum or if ‘from’ date is greater than ‘to’ date. The message will contain a specific cause description.

SpikeInvalidPostbackUrlException

Thrown if the callback URL is not a valid URL, does not have HTTPS schema, or is not a sub-URL of the main callback URL configured for the application. The exception message will contain a specific reason.

SpikePackException

Thrown when a connection cannot be serialised to a packed state. The exception cause will contain the underlying error.

SpikePermissionsNotGrantedException

Thrown when Spike SDK is unable to access the data due to permissions not being granted.

SpikeServerException

Whenever any operation with the Spike backend fails and the error does not fall into any of the above exceptions. The exception cause will contain the underlying error.

SpikeUnpackException

Thrown when a connection cannot be deserialised from a packed state. The exception cause will contain the underlying error.