iOS (Swift)
SDK Installation
RealTime streaming on iOS is only available to those registered on the Apple Developer Program
Summary:
Add https://github.com/tryterra/TerraRTiOS as a package dependency
In your info.plist, add:
Privacy - Bluetooth Always Usage Description: Justification for BLE usage (shown to the user when requesting permission)Privacy - Motion Usage Description: Justification for phone motion sensor usage (shown to the user when requesting permission - only add if using phone sensors for streaming)
When the app is launched for the first time, these permissions will be requested from the user.
Connection setup/management
The SDK revolves around the TerraRT class, which manages Bluetooth connections and real-time data streaming.
SDK Initialization
To do this, run the TerraRT manager initialization function as below:
Initializing a Connection
Register the phone by calling initConnection.
This connects the phone to Terra, allows you to use other SDK functions, and allows it to be a producer once a wearable is connected later on.
Use the terraRT instance created above and call initConnection as below:
To generate the token, make the below call from your backend
Connecting a device
iOS devices only support connection via BLE or Apple Watch's custom bluetooth protocol. For the Apple Watch connection, refer to the guide below
You may use an example as below to call the startBluetoothScan function, which pulls up a modal allowing the user to select a wearable to connect
Streaming Real-Time Data
Once the phone is registered and you've connected a wearable, you can stream data from the wearable to your mobile app.
Start Streaming
You can start receiving data from the connected wearable's stream by calling startRealtime with the following signature
you can also stop the stream and disconnect the wearable once done
WatchOS Integration
Setup
Create a WatchOS target within your iOS project:
File -> New -> Target -> Watch App for iOS App.
Enable HealthKit and Background Modes for the WatchOS app, and add the following privacy keys to your
Info.plist:Privacy - Health Share Usage DescriptionPrivacy - Health Update Usage DescriptionPrivacy - Health Records Usage Description
Start Streaming from WatchOS
To begin streaming data from Apple Watch, call the startStream() function from within the Watch app. This will initiate data streaming from the Watch sensors to the paired iOS app.
On the iOS app, you'll equivalently need to listen for updates from the WatchOS app:
Reading a data stream on iOS will only be possible if you have previously created a connection
Streaming Workouts from WatchOS
The Apple Watch app can also manage a workout session, during which recording frequency is enhanced.
To manage a workout session, you can use the following convenience functions:
From the Watch:
startExercise
stopExercise
resumeExercise
pauseExercise
From the iOS app:
pauseWatchOSWorkout
resumeWatchOSWorkout
stopWatchOSWorkout
Here is an example template of the watchOS app using the functions outlined above
Last updated
Was this helpful?