Flutter SDK
Connect a BLE wearable or Apple Watch to your Flutter app with terra_flutter_rt, then stream its data to Terra.
Install the SDK
Add
terra_flutter_rtto yourpubspec.yaml:
dependencies:
terra_flutter_rt: ^X.X.XiOS: In your
Info.plist, add:Privacy - Bluetooth Always Usage Description: Justification for BLE usagePrivacy - Motion Usage Description: Justification for motion sensor usage (required if streaming accelerometer, gyroscope, or step data from the phone's built-in sensors viaConnection.apple)
Android: Permissions are requested automatically by the SDK on initialization.
Initialize the SDK
import 'package:terra_flutter_rt/terra_flutter_rt.dart';
import 'package:terra_flutter_rt/types.dart';
await TerraFlutterRt.init('YOUR_DEV_ID', 'YOUR_REFERENCE_ID');Register the phone
Register the phone with Terra using an authentication token generated from your backend:
To generate the token, make the below call from your backend:
Same as POST /auth/tokens, under the older path that shipped mobile SDK versions call.
- Stays available indefinitely; new integrations should use
POST /auth/tokens.
Your API key for authentication
Your developer ID for authentication and tracking
Your identifier for the end user this token is being issued for. When supplied, the token is bound to it and redemption ignores the reference_id the SDK sends, so a token cannot be used to attach a device to a different user. Omit to keep the SDK-supplied value.
user-42200
success250c68b9c21b78e40e7a3285a2d538d3bc24aabd3b4c76a782fb0a571ca4501d0Example: 180The credential is missing, invalid, expired or revoked (code unauthorized).
The credential is not authorized (code scope_missing or audience_restricted), the environment is disabled (environment_disabled), or the required product entitlement is missing (entitlement_required).
A referenced resource does not exist on Terra's end.
The credential or the product access could not be checked (code authorization_unavailable). Retry after the service recovers.
POST /api/v2/auth/generateAuthToken HTTP/1.1
Host: access.tryterra.co
x-api-key: YOUR_API_KEY
dev-id: YOUR_API_KEY
Accept: */*
{
"status": "success",
"token": "250c68b9c21b78e40e7a3285a2d538d3bc24aabd3b4c76a782fb0a571ca4501d",
"expires_in": 180
}Scan for devices
Two approaches depending on your needs:
Use the built-in scan widget
On Android, this shows a native device picker. On iOS, use the iOSScanView widget.
Scan programmatically
Discover devices one by one and connect manually:
Start streaming
Once a device is connected, start streaming data to your app via a callback:
To stream to Terra's server instead, see Stream to Terra.
Stop streaming and disconnect
Stream from an Apple Watch
To stream data from an Apple Watch:
You can also control workout sessions on the watch from your Flutter app:
Stream to Terra
Once data from the wearable reaches your app, you can send it to Terra's websocket server. Your backend then receives it through a backend consumer.
The local stream uses startRealtimeToApp. To send data to Terra's server instead, use startRealtimeToServer with a streaming token:
Your backend should generate the streaming token by calling:
Returns a token that opens a producer connection for one user, to push that user's live samples.
- By default the token is single-use and short-lived.
- With
reusable=truethe same token opens a connection on every reconnect until it expires or is revoked withPOST /auth/user/revoke, and the response addsexpires_in.
Your API key for authentication
Your developer ID for authentication and tracking
The user the token is for: their Terra user ID (a UUID, returned when they connect), their connection_id, or a streaming test user's t_ ID from the dashboard's "generate test data" tool. A user who is not connected under your developer ID, or a test user that has expired, gets 403.
Set to true for a reusable token instead of a single-use one. Defaults to false. A test user (t_ ID) cannot have a reusable token and gets 400.
falseToken generated.
Token to present in the IDENTIFY frame. A reusable token starts with rt..
OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eESeconds until the reusable token expires, never later than the credential that minted it. Present only with reusable=true.
28800Missing id parameter, reusable=true for a test user, or missing/malformed authentication headers.
The credential is missing, invalid, expired or revoked (code unauthorized).
The credential lacks the scope this operation requires (code scope_missing) or is not granted for this dev-id (audience_restricted); or, as a message, invalid, inactive or not enabled for streaming, or the user does not belong to this developer.
Token generation failed.
Authorization is temporarily unavailable (code authorization_unavailable), or credential validation failed (a message). Retry later.
POST /auth/user?id=text HTTP/1.1
Host: ws.tryterra.co
x-api-key: YOUR_API_KEY
dev-id: YOUR_API_KEY
Accept: */*
{
"token": "OTYwNWFi5ZWQMTAxMjg0Y2Qw.gzrPzZcS3Gy8QDOxbiPRwu30PTB3VxW0eE",
"expires_in": 28800
}Last updated
Was this helpful?