Android

Prerequisites

Ensure you have the following before you begin:

  1. Android Studio: The latest stable version.

  2. Terra Account: Access to your Terra Developer account and API key.

  3. 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

Always initialize the TerraRT class to begin using the SDK.

Do so every time the app is opened or brought into the foreground.

To do this, run the TerraRT manager initialization function as below:

import android.app.Activity
import android.content.Context
import com.terra.TerraRT

class MainActivity : Activity() {
    private lateinit var terraRT: TerraRT

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // Replace with your actual developer ID and reference ID
        val developerId = "yourDeveloperId"
        val referenceId: String? = "yourReferenceId"

        // Initialize TerraRT SDK
        terraRT = TerraRT(devId = developerId, context = this, referenceId = referenceId) { success ->
            if (success) {
                println("TerraRT initialized successfully")
                // Proceed with other setup tasks
            } else {
                println("Failed to initialize TerraRT")
                // Handle failure case
            }
        }
    }
}

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

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.

Receiving a real time data stream depends on the device being configured to broadcast the data it is collecting through BLE or ANT+

Start Real-Time Streaming

Stop Real-Time Streaming

To stop real-time streaming for the connected device, use the following method:

This stops data streaming for the specified connection type.

Disconnect Device

To disconnect a connected device:

This disconnects the BLE device or any specified connection type.

Example Usage

Full Example: Managing a BLE Connection and Streaming Data

Explanation:

  1. Initialization: The SDK is initialized using an authentication token.

  2. Device Connection: A BLE device is scanned and connected.

  3. Real-Time Streaming: Heart rate and steps data are streamed in real-time.

  4. Stop Streaming: After a delay of 5 seconds, the stream is stopped, and the device is disconnected.

Last updated

Was this helpful?