Spike SDK usage
Step 1 - Create Spike connection
To set up the Spike SDK create SpikeConnectionV3
instance with your Spike application id, application user id and signature unique to each of your apllication users (more on generating signatures here):
import Spike from 'react-native-spike-sdk';
const connection = await Spike.createConnectionAPIv3(
"applicationId",
"signature",
"endUserId"
);
Step 2 - Ask user for permissions
If you plan to use Android Health Connect and/or Apple HealthKit data providers you should first get users permission to use their data:
// This call may present user with OS native modal asking for read
// permissions needed to get steps and distance data. If you want to
// present it only once, you should list all the statistic types you
// plan to use. Otherwise, you can call this only before you actually
// want to use the data.
await spikeConnection.requestHealthPermissions({
statisticTypes: [StatisticsType.steps, StatisticsType.distanceTotal]
});
- for Android there are more methods that let you get permissions granted by user, check if Health Connect is installed or should be updated, etc. If you are OK with SpikeSDK handling these, you can use
requestHealthPermissions
only. On iOS requestHealthPermissions
is the only method that has to be called. Keep in mind, that you should do it before trying to read data from Android Health Connect or Apple HealthKit.
Step 3 - Get data
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.
Statistics
Now you can read hourly statistics data of steps and distance for today:
const now = new Date();
const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
const end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
const statistics = await spikeConnection.getStatistics(
[StatisticsType.steps, StatisticsType.distanceTotal],
start,
end,
StatisticsInterval.hour,
new StatisticsFilter(false, [Provider.apple, Provider.healthConnect])
)
where:
enum StatisticsType {
steps = "steps",
distanceTotal = "distance_total",
distanceWalking = "distance_walking",
distanceCycling = "distance_cycling",
distanceRunning = "distance_running",
caloriesBurnedTotal = "calories_burned_total",
caloriesBurnedBasal = "calories_burned_basal",
caloriesBurnedActive = "calories_burned_active",
}
enum StatisticsInterval {
day,
hour
}
// Filter
export class StatisticsFilter {
excludeManual: boolean = false
providers: Provider[] | undefined
constructor({
excludeManual = false,
providers = undefined
}: StatisticsFilterConstructorParameters) {
this.excludeManual = excludeManual;
this.providers = providers;
}
}
// Result:
interface Statistic {
start: string;
end: string;
duration: number;
type: StatisticsType;
value: number;
unit: Unit;
rowCount: number | null;
recordIds: UUID[] | null;
}
Records
const now = new Date();
const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
const end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
const records = await spikeConnection.getRecords({
ofTypes: metricTypes,
from: start,
to: end,
filter: new StatisticsFilter({
excludeManual: false,
providers: [Provider.apple, Provider.healthConnect]
})
})
enum MetricType {
caloriesBurnedActive = "calories_burned_active",
caloriesBurnedBasal = "calories_burned_basal",
caloriesBurned = "calories_burned",
distanceTotal = "distance",
distanceWalking = "distance_walking",
distanceCycling = "distance_cycling",
distanceRunning = "distance_running",
distanceWheelchair = "distance_wheelchair",
distanceSwimming = "distance_swimming",
stepsTotal = "steps",
}
// Result:
interface SpikeRecord {
recordId: UUID;
inputMethod: InputMethod | null;
startAt: string;
endAt: string | null;
modifiedAt: string;
duration: number | null;
provider: Provider | null;
providerSource: ProviderSource | null;
isSourceAggregated: boolean | null;
source: RecordSource | null;
metrics: { [key: string]: number } | null;
activityTags: ActivityTag[] | null;
activityType: ActivityType | null;
sessions: ActivityEntry[] | null;
laps: ActivityEntry[] | null;
segments: ActivityEntry[] | null;
splits: ActivityEntry[] | null;
samples: ActivitySamples[] | null;
routePoints: ActivitySamples[] | null;
}