Android

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 stream data from a wearable device to your app

Setting up a connection

You'll have previously set up a stream between a wearable device and your app using the startRealTime function. 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

Last updated

Was this helpful?