Terra Docs
Dashboard
  • Docs
  • API Reference
  • Changelog
  • Get started ⚡️ Choose your integration
  • Health & Fitness API
    • Overview
    • Set up your Integrations
    • Connect a User
    • Receive data updates
    • Request Historical data
    • Write data
    • Debugging FAQ
    • Mobile-Only Sources: Apple, Samsung, Google Fit
      • iOS (Swift)
      • Android (Kotlin)
      • React Native
      • Flutter
  • Streaming API
    • Getting Started
    • Wearable -> Your app
      • iOS (Swift)
      • Android
    • Your app -> Terra
      • iOS (Swift)
      • Android
    • Terra -> Your Backend
  • Teams API
  • Biomarkers API - Upcoming
Powered by GitBook
On this page

Was this helpful?

  1. Streaming API
  2. Your app -> Terra

Android

PreviousiOS (Swift)NextTerra -> Your Backend

Last updated 6 months ago

Was this helpful?

Once you've started receiving data from a device into your app, you can start sending this data to Terra's websocket server (the broker).

This will later allow you to receive it on your backend, and process it as per your requirements.

Prerequisite

Before following the steps below, make sure you've followed the guide to

Setting up a connection

You'll have previously set up a stream between a wearable device and your app . In order to to send this data back to the broker, you'll simply use a variant of the startRealTime function

import com.terra.TerraRT
import com.terra.TerraRT.Connections
import com.terra.TerraRT.DataTypes
import com.terra.TerraRT.Update
import android.os.Handler
import android.os.Looper

val terraRT = TerraRT()

val token = "yourAuthToken"  // Replace with your actual token

// Initialize TerraRT SDK connection
terraRT.initConnection(token) { success ->
    if (success) {
        println("TerraRT initialized successfully!")

        // Start device scan for BLE connection
        terraRT.startDeviceScan(type = Connections.BLE) { scanSuccess ->
            if (scanSuccess) {
                println("Device connected successfully!")

                // Start streaming real-time data using the new function signature
                val dataTypes: Set<DataTypes> = setOf(DataTypes.HEART_RATE, DataTypes.STEPS)
                terraRT.startRealtime(
                    type = Connections.BLE, 
                    dataTypes = dataTypes, 
                    token = token, 
                    updateHandler = { update ->
                        handleUpdate(update)
                    }, 
                    connectionCallback = { connectionSuccess ->
                        if (connectionSuccess) {
                            println("Real-time connection successfully established!")
                        } else {
                            println("Failed to establish real-time connection.")
                        }
                    }
                )

                // Stop streaming after 5 seconds for demo purposes
                Handler(Looper.getMainLooper()).postDelayed({
                    terraRT.stopRealtime(type = Connections.BLE)
                    println("Real-time streaming stopped.")

                    // Optionally disconnect the device
                    terraRT.disconnect(type = Connections.BLE)
                    println("Device disconnected.")
                }, 5000)
            } else {
                println("Failed to connect to the device.")
            }
        }
    } else {
        println("Failed to initialize TerraRT.")
    }
}

fun handleUpdate(update: Update) {
    println("Received update at timestamp: ${update.ts}")
    println("Update type: ${update.type}")
    update.`val`?.let { value ->
        println("Value: $value")
    }
}

Simply passing in a token from the following endpoint will allow you to stream the same data you were reading previously within your app, and have it sent to the broker

Make sure to pass in the correct user ID in the id query paramter in the call below. You may retrieve that user the getUserId function in the SDK, when about to ask your backend to create the token

stream data from a wearable device to your app

Stream - Generate user token

post

Endpoint for generation of a token for a user (producer) connection

Authorizations
Query parameters
idstringOptional

The ID of the user to generate a token for

Header parameters
dev-idstringRequired

your developer ID

x-api-keystringRequired

your API key

Responses
200
Successful response
application/json
403
Forbidden
text/plain
post
POST /auth/user HTTP/1.1
Host: ws.tryterra.co
x-api-key: text
dev-id: text
Accept: */*
{
  "token": "OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eE"
}
  • Setting up a connection
  • POSTStream - Generate user token
using the startRealTime function