Spike Flutter SDK is a library on top of Apple HealthKit and Android Health Connect that
helps with the extraction of data.
pushes data to SpikeAPI and delivers standardized data.
<manifest>
<uses-permission android:name="android.permission.health.READ_NUTRITION"/>
<uses-permission android:name="android.permission.health.READ_ACTIVE_CALORIES_BURNED"/>
<uses-permission android:name="android.permission.health.READ_TOTAL_CALORIES_BURNED"/>
<uses-permission android:name="android.permission.health.READ_STEPS"/>
<uses-permission android:name="android.permission.health.READ_DISTANCE"/>
<uses-permission android:name="android.permission.health.READ_ELEVATION_GAINED"/>
<uses-permission android:name="android.permission.health.READ_RESTING_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE_VARIABILITY"/>
<uses-permission android:name="android.permission.health.READ_FLOORS_CLIMBED"/>
<uses-permission android:name="android.permission.health.READ_BASAL_METABOLIC_RATE"/>
<uses-permission android:name="android.permission.health.READ_SLEEP"/>
<uses-permission android:name="android.permission.health.READ_HEART_RATE"/>
<uses-permission android:name="android.permission.health.READ_EXERCISE"/>
<uses-permission android:name="android.permission.health.READ_SPEED"/>
<uses-permission android:name="android.permission.health.READ_POWER"/>
…
</manifest>
iOS 13.0+
Xcode 13.0+
Health Kit Reporter 2.1.0+
Background fetch 1.0.3+
On iOS, go to your target's Signing & Capabilities section and add HealthKit. Allow Background fetch and Background Delivery.
On iOS, add HealthKit permissions descriptions to your Info.plist file.
<key>NSHealthShareUsageDescription</key>
<string>We will use your health information to better track workouts.</string>
<key>NSHealthUpdateUsageDescription</key>
<string>We will update your health information to better track workouts.</string>
...
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>
...
The package can be found here.
To add the SDK to your Android project, you have to add the following to your project's build.gradle file in the repositories block.
buildscript {
repositories {
maven {
url 'https:
}
}
}
allprojects {
repositories {
maven {
url 'https:
}
}
}
To set up the Flutter SDK, please 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.
import 'package:spike_flutter_sdk/spike_api.dart';
await SpikeSdk.ensurePermissionsAreGranted();
const authToken = 'fa0b3803-6068-4e22-9788-eccce210d30c';
const appId = 'ea9e03f5-be45-49fb-bf11-47a88c184c3b';
const customerEndUserId = 'end_user_1';
const postbackURL = 'https://my_webhooks_url.com/';
const host = 'https://api.spikeapi.com';
final conn = await SpikeSdk.createConnection(
host: host,
appId: appId,
authToken: authToken,
customerEndUserId: customerEndUserId,
);
final webHookConnection = await SpikeSDK.createConnection(
host: host,
appId: appId,
authToken: authToken,
customerEndUserId: customerEndUserId,
postbackURL: postbackURL,
);
Once a connection has been created data can be retrieved using the extractData method. The below example shows how to retrieve daily steps for today which are returned as part of the activities summary.
final stepsData = await connection.extractData(SpikeDataType.steps);
if (stepsData.data.isNotEmpty) {
print("Steps (Count): ${stepsData.data.length}");
print("Steps[0]: ${stepsData.data[0].value}");
}
final heartJobDetails = await webHookConnection.extractAndPostData(SpikeDataType.heart);
Internally, the Flutter SDK supports logging on various levels to assist with troubleshooting. However, to avoid imposing additional third-party dependencies on library users, the SDK expects a concrete logging implementation to be provided externally. This can be done by implementing the SpikeEventLogger class and providing it when creating or unpacking a connection.
Below is an example of how to implement a simple console logger.
abstract class SpikeEventLogger {
String getId();
void onEvent(final SpikeEvent event);
}
class EventsLogger extends SpikeEventLogger {
static final EventsLogger _singleton = EventsLogger._internal();
factory EventsLogger() {
return _singleton;
}
EventsLogger._internal();
@override
String getId() => 'total-tracker';
@override
Future<void> onEvent(SpikeEvent event) async {
print('Event ${event.type}. Date: ${event.date}.');
await EventsLogRepository().add(event);
}
}
Background delivery enables asynchronous data delivery to the customer backend by means of webhooks. It enables data updates to be sent to the backend even when the application is hidden or closed. Background delivery is only supported on iOS devices at the moment.
Below is an example of how to start background delivery for daily summaries.
final webHookConnection = await SpikeSdk.createConnection(
appId: 'my_app_id'
authToken: 'my_app_access_token',
customerEndUserId: 'my_user_id',
callbackUrl: 'https://example.com/data',
);
await SpikeSDK.enableBackgroundDelivery(
connection: webHookConnection,
types: [SpikeDataType.summary, SpikeDataType.heart],
intervalSeconds: 30,
);
After the background delivery is enabled, all relevant data is stored in memory, and you do not need to enable it again when the app opens again.