- Homepage
- App Integration
App Integration
The Moonsense SDK is built to be incorporated into your own products whether that be an application or your own SDK. This article will walk through the basics of incorporating the Moonsense SDK into your own application. Regardless of the platform you're building for the pattern for incorporating the SDK is the same which is the focus of this article. However, there are some platform specifics that need to be accounted for and to make this task easier, we have created sample applications for Android, iOS, and Web that provide specifics on how to integrate the SDK. Those sample applications can be found here:
Setup
There a few steps that need to be taken first before you can begin building your project.
Get A Public Token
The first step is to get a Public Token from the Moonsense Console. The token will be included in the SDK initialization process for the SDK and is how you will associate sessions with your project.
Create a Project
Create a project for your platform as you normally would. For the purpose of this article we will assume you are using Kotlin for Android, Swift for iOS, and that you are setting up a project in a javascript framework that uses NPM for Web.
Setup Access to the SDK Private Repository
The SDK libraries are stored in a private repository. You will first need to get an Access Token from Moonsense then you will need to get setup to be able to access the repo.
// Add the following to your project's build.gradle.kts file
repositories {
maven {
val repoAccessToken = "ADD_TOKEN_HERE"
url = uri("https://dl.moonsense.io/$repoAccessToken/sdk/maven")
}
}
# Add the following to ~/.netrc
machine dl.moonsense.io
login token
password <REPO_ACCESS_TOKEN>
protocol https
# Add the following to ~/.npmrc or the .npmrc file in your project
@moonsense:registry=https://npm.moonsense.io/sdk/
//npm.moonsense.io/sdk/:_authToken=<repo_access_token>
Install the SDK
Next you'll need to install the SDK into your project.
// Add the following to your project's gradle file
implementation("io.moonsense:android-sdk:<latest_version>")
# The Moonsense iOS SDK is available as a Swift Package. Simply use the the link to the public repo, https://github.com/moonsense/moonsense-ios-sdk, as the Package URL when adding the Swift Package to your project.
npm install --save @moonsense/moonsense-web-sdk
Initialize the SDK
Now that the project is setup and the Moonsense SDK is included, the SDK needs to be initilized. This process sets parameters that are to be used across the lifecycle of the SDK usage. At a minimum the SDK needs to be passed the PUBLIC_TOKEN
used to associate the application with your Project in the Moonsense Console. The initialize process also supports more configuration options that are detailed more in the platform specific code documentation:
import io.moonsense.sdk.Moonsense
// Add the following to the Application's or Activity's onCreate() function
override fun onCreate() {
Moonsense.initialize(
context,
"<YOUR_PUBLIC_TOKEN_HERE>"
)
}
import MoonsenseSDK
// ... your code here ...
Moonsense.initialize(publicToken: <YOUR_PUBLIC_TOKEN_HERE>, delegate: self)
import { Moonsense } from '@moonsense/moonsense-web-sdk';
// ... your code here ...
const moonsenseSdk = new Moonsense({
publicToken: "<YOUR_PUBLIC_TOKEN_HERE>"
});
Start a Session
After the SDK is initialized, you can start a Session. The Session will record all available sensors and will run for, at most, the specified duration.
// Creates a session that will record for up to 30 seconds
val session = Moonsense.startSession(
duration = TimeUnit.SECONDS.toMillis(30L),
labels = listOf("SampleApp")
)
var session: Session?
// ... your code here ...
// Creates a session that will record for up to 30 seconds
session = try? Moonsense.startSession(duration: 30, labels: ["SampleApp"])
// Creates a session that will record for up to 30 seconds
const session = moonsenseSdk.startSession({
duration: 30000,
labels: ["SampleApp"],
});
Stop a Session
The Session will automatically stop recording when the duration specified in startSession(...)
is reached. However, there are two possible methods to end a Session. Method 1 is to call session.stopSession()
on the session object which will stop this specific session. Method 2 is to tell the SDK to stopAllSessions()
.
// Method 1: Stop a specific session
session.stopSession()
// Method 2: Stop all active sessions
MoonsenseSDK.stopAllSessions();
// Method 1: Stop a specific session
session.stopSession()
// Method 2: Stop all active sessions
Moonsense.stopAllSessions();
// Method 1: Stop a specific session
session.stopSession()
// Method 2: Stop all active sessions
moonsenseSdk.stopAllSessions();