React Native
Prerequisites
Install the
terra-rtpackage:
npm install terra-rtiOS: 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 viaConnections.APPLE)
iOS: Run
pod installin your/iosdirectory.Android: Permissions are requested automatically by the SDK on initialization.
SDK Initialization
Set up event emitters for receiving data, then initialize:
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
import {
initTerra, initConnection, startDeviceScan, startRealtime,
stopRealtime, disconnect, getUserId,
Connections, DataTypes,
} from 'terra-rt';
import type { Update, Device, SuccessMessage } from 'terra-rt';
// Set up event listeners (do this once, e.g. in useEffect)
const updateEmitter = new NativeEventEmitter(NativeModules.UpdateHandler);
updateEmitter.addListener('Update', (update: Update) => {
console.log(`${update.type}: ${update.val}`);
});
const deviceEmitter = new NativeEventEmitter(NativeModules.DeviceHandler);
deviceEmitter.addListener('Device', (device: Device) => {
console.log(`Found device: ${device.name}`);
});
const connectionEmitter = new NativeEventEmitter(NativeModules.ConnectionHandler);
connectionEmitter.addListener('ConnectionUpdate', (connected: boolean) => {
console.log(`Websocket connected: ${connected}`);
});
// Initialize the SDK
const result = await initTerra('YOUR_DEV_ID', 'YOUR_REFERENCE_ID');
if (!result.success) {
console.error('Failed to initialize:', result.error);
}Initializing a Connection
Register the device with Terra using an authentication token generated from your backend:
To generate the token, make the below call from your backend:
Deprecated spelling of POST /auth/tokens, which it is identical to. Kept indefinitely because shipped mobile SDK versions call this path; 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: 180A referenced resource does not exist on Terra's end.
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
}Device Scanning
BLE device scanning works differently on iOS and Android:
On Android, use startDeviceScan which shows a built-in device picker:
On iOS, startDeviceScan is not supported. Instead, use the BLWidget native view component:
Render this component when you want to show the BLE scanner. It displays a list of discovered devices and handles connection automatically.
Platform-conditional pattern:
Real-Time Data Streaming
Once a device is connected, start streaming data. Updates arrive via the Update event emitter you set up earlier.
To also stream to Terra's server, see Your App → Terra.
Stop & Disconnect
WatchOS Integration
To stream data from an Apple Watch, call connectWithWatchOS() on the iOS companion app side, then start streaming with Connections.WATCH_OS:
Last updated
Was this helpful?