RT Streaming - Flutter

Using the package

The Terra Real Time package offers the capability to stream data either in your app directly using a callback function, or to your server using websockets.

Import the package in your app file:

import 'package:terra_flutter_rt/terra_flutter_rt.dart';
import 'package:terra_flutter_rt/types.dart';
import 'package:terra_flutter_rt/ios_controller.dart';

The first line is for all the functionality. The types are for the different data types (connections, requested data types, and received data type). The iOS controller is for the iOS scanning controller UI.

Everytime your app opens, you need to initialise the library:

await TerraFlutterRt.init(devId, "reference_id");

The first time ever you want to use the package, you need to register the device with Terra using initConnection. You can generate an SDK token from here using your Terra dev credentatials:

await TerraFlutterRt.initConnection(sdktoken);

and then get the user id using getUserId

const userId = await TerraFlutterRt.getUserId();

The packages support streaming from the following wearable types:

enum Connection { ble, apple, wearOs, android, ant }

and supports streaming the following data types:

enum DataType {
  heartRate,
  ecg,
  steps,
  hrv,
  calories,
  location,
  speed,
  distance,
  stepsCadence,
  floorsClimbed,
  gyroscope,
  acceleration
}

An example using a connection and a set of data to be streamed:

const connection = Connection.ble;
const datatypes = [DataType.heartRate];

For BLE connections and ANT+, you can pull a scanning UI for the user to select a device:

if (connection == Connection.ble ||
        connection == Connection.wearOs ||
        connection == Connection.ant ||
        connection == Connection.allDevices) {
      await TerraFlutterRt.startDeviceScan(connection);
	}

you can optionally cache the devices. If set to true, this options will not pull the scan screen if one of the cached devices is found:

await TerraFlutterRt.startDeviceScan(connection, useCache: true);
With the device and dataypes set, you can now start streaming. To stream locally in your app directly, you can pass a callback function that will get triggered with the data:

void dataCallback(Update data) {
  print("Got data in app");
  print(data.ts);
  print(data.type.datatypeString);
  print(data.val);
  print(data.d);
}

// .......

await TerraFlutterRt.startRealtimeToApp(connection, datatypes, dataCallback);

Example payloads:

{
  "val":86,
  "type":"HEART_RATE",
  "ts":"2022-10-24T09:15:25Z"
}

{
  "d":[1.373291015625E-4,-3.96728515625E-4,-1.068115234375E-4],
  "ts":"2022-10-24T09:20:27.985Z",
  "type":"GYROSCOPE"
}

To stream to your server using Websockets, you need a websocket token generated from here:

await TerraFlutterRt.startRealtimeToServer(connection, datatypes, websockettoken);

Example payloads:

{
	"op":5,
  "d":{"ts":"2022-10-24T09:18:27.729Z","val":83.0},
  "uid":"66fbb25f-e1d2-44d1-bc4d-6b4844bd0928",
  "seq":16073,
  "t":"HEART_RATE"
}

{
  "op":5,
  "d":{"d":[0.0049285888671875,-3.662109375E-4,-2.899169921875E-4],
  "ts":"2022-10-24T09:19:06.328Z"},
  "uid":"66fbb25f-e1d2-44d1-bc4d-6b4844bd0928",
  "seq":16088,
  "t":"GYROSCOPE"
}

Finally, you can stop streaming and disconnect the device on command:

await TerraFlutterRt.stopRealtime(connection);
await TerraFlutterRt.disconnect(connection);