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
Endpoint for generation of a token for a user (producer) connection
The ID of the user to generate a token for
your developer ID
your API key
POST /auth/user HTTP/1.1
Host: ws.tryterra.co
x-api-key: text
dev-id: text
Accept: */*
{
"token": "OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eE"
}
Last updated
Was this helpful?