androidAndroid

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.

circle-info

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 send this data to the broker, use the startRealtime overload that accepts a token:

import co.tryterra.terrartandroid.TerraRT
import co.tryterra.terrartandroid.enums.Connections
import co.tryterra.terrartandroid.enums.DataTypes
import co.tryterra.terrartandroid.models.Update

// Assumes terraRT is already initialized and a device is connected
// (see the "Connect Wearable to SDK" guide)

val token = "yourStreamingToken"  // Generated from your backend (see below)
val dataTypes = setOf(DataTypes.HEART_RATE, DataTypes.STEPS)

// Stream to BOTH your app AND Terra's websocket server
terraRT.startRealtime(
    type = Connections.BLE,
    dataTypes = dataTypes,
    token = token,
    updateHandler = { update ->
        println("${update.type}: ${update.`val`} at ${update.ts}")
    },
    connectionCallback = { connected ->
        println("Websocket connected: $connected")
    }
)

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.

circle-info

Make sure to pass in the correct user ID in the id query parameter in the call below. You may retrieve that user ID using the getUserId function in the SDK.

Stream - Generate user token

post

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

Authorizations
x-api-keystringRequired
dev-idstringRequired
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
chevron-right
200

Successful response

application/json
tokenstringOptionalExample: OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eE
post
/auth/user

Last updated

Was this helpful?