React Native

Prerequisites

  1. Install the react-native-terra-rt-react package:

npm install react-native-terra-rt-react
  1. iOS: In your Info.plist, add:

    • Privacy - Bluetooth Always Usage Description: Justification for BLE usage

    • Privacy - Motion Usage Description: Justification for motion sensor usage (required if streaming accelerometer, gyroscope, or step data from the phone's built-in sensors via Connections.APPLE)

  2. iOS: Run pod install in your /ios directory.

  3. Android: Permissions are requested automatically by the SDK on initialization.

SDK Initialization

Initialize the SDK every time the app is opened or brought into the foreground.

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 'react-native-terra-rt-react';
import type { Update, Device, SuccessMessage } from 'react-native-terra-rt-react';

// 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:

Device Scanning

BLE device scanning works differently on iOS and Android:

On Android, use startDeviceScan which shows a built-in device picker:

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:

The watchOS app itself must be written in native Swift using the Terra class from the TerraRTiOS framework. See the iOS WatchOS guide for the watch-side setup.

Last updated

Was this helpful?