Android
Prerequisites
Ensure you have the following before you begin:
Android Studio: The latest stable version.
Terra Account: Access to your Terra Developer account and API key.
Gradle Dependency: Ensure you include the TerraRT SDK in your project by adding the necessary dependencies.
implementation 'co.tryterra:terra-rtandroid:X.X.X'based on the latest version on maven central
SDK Initialization
To do this, run the TerraRT manager initialization function as below:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import co.tryterra.terrartandroid.TerraRT
class MainActivity : AppCompatActivity() {
private lateinit var terraRT: TerraRT
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val developerId = "yourDeveloperId"
val referenceId: String? = "yourReferenceId"
terraRT = TerraRT(
devId = developerId,
context = this,
referenceId = referenceId
) { success ->
if (success) {
println("TerraRT initialized successfully")
} else {
println("Failed to initialize TerraRT")
}
}
}
}Initializing a Connection
Register the phone by calling initConnection.
This connects the phone to Terra, allows you to use other SDK functions, and allows it to be a producer once a device is connected later on.
Use the terraRT instance created above and call initConnection as below using an authentication token.
To generate the token, make the below call from your backend
Deprecated spelling of POST /auth/tokens, which it is identical to. Kept indefinitely because shipped mobile SDK versions call this path; new integrations should use POST /auth/tokens.
Your API key for authentication
Your developer ID for authentication and tracking
Your identifier for the end user this token is being issued for. When supplied, the token is bound to it and redemption ignores the reference_id the SDK sends, so a token cannot be used to attach a device to a different user. Omit to keep the SDK-supplied value.
user-42200
success250c68b9c21b78e40e7a3285a2d538d3bc24aabd3b4c76a782fb0a571ca4501d0Example: 180A referenced resource does not exist on Terra's end.
POST /api/v2/auth/generateAuthToken HTTP/1.1
Host: access.tryterra.co
x-api-key: YOUR_API_KEY
dev-id: YOUR_API_KEY
Accept: */*
{
"status": "success",
"token": "250c68b9c21b78e40e7a3285a2d538d3bc24aabd3b4c76a782fb0a571ca4501d",
"expires_in": 180
}Device Connection Management
Once initialized, you can scan for devices and connect to them using the TerraRT SDK.
You can scan for devices using
BLE
ANT+ (if supported by the Android phone used)
Start Device Scan
You can start scanning for devices and allow the user to connect to a wearable device using startDeviceScan.
You can choose whether to use cached devices (ones you've connected to before) or show a connection widget if no cached device is found.
Real-Time Data Streaming
Once the device is connected, you can start streaming real-time data such as heart rate, steps, and more.
Start Real-Time Streaming
Stop Real-Time Streaming
Disconnect Device
Example Usage
Full Example: Managing a BLE Connection and Streaming Data
Last updated
Was this helpful?