Skip to main content

SDK for iOS

Swift documentation for iOS apps

Required tools

SDK Version compatibility

  • iOS's deployment minimum target: 13.1
  • Swift version is 5.0.

SDK installation

RookMotion SDK is available through CocoaPods. To install it, follow the next steps:

  1. Creat a Podfile.
pod init
  1. Open the Podfile
open .Podfile -a xcode
  1. Add the following line to Podfile
    pod 'RookMotionSDK', :git => "https://gitlab.com/RookMotionforclients/RookMotion-SDK-Cocoa.git"
  1. Add the following lines at the end of the Podfile
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['BUILD_LIBRARY_FOR_DISTRIBUTION'] = 'YES'
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
end
end
end
  1. run pod install
pod install

SDK Configuration

Add client_key and token_level to AppDelegate.swift file as follows:

  1. Import RookMotionLink statement:
import RookMotionSDK
  1. Add the following lines to aplication(_:didFinishLaunchingWithOptions:) method, replacing YOUR_KEY and YOUR_TOKEN with your credential:
  2. Set the urlBase to sandbox environment or production environment.
  3. Set the urlRemote to sandbox environment or production environment for remote trainings.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let clientKey = "YOUR_KEY"
let tokenLevel = "YOUR_TOKEN"
let urlBase = "YOUR_BASE_URL"
let urlRemote = "YOUR_REMOTE_URL"

RMSettings.shared.setCredentials(client_key: clientKey, token: tokenLevel)
RMSettings.shared.setUrlApi(with: urlBase)
RMSettings.shared.setUrlRemote(with: urlRemote)
RMSettings.shared.initRookMotionSDK()

return true
}

Permissions

The following permissions must be set into the Info.plist file.

KeyTypeString
Privacy - Bluetooth Always Usage DescriptionStringAllow Bluetooth access to connect with your training sensors devices.
Privacy - Bluetooth Peripheral Usage DescriptionStringAllow Bluetooth access to connect with your training sensors devices.

Signing & Capabilities

The following capabilities must be enabled for the project

  • Background Modes
    • Uses Bluetooth LE accessories

SDK: Bluetooth Manager

Connect and gather data from standard BT Heart Rate Devices.

💡*Standard Heart Rate Devices, refers to devices with Heart Rate Measurement capability and Bluetooth exposure of the measurements according to Heart Rate profile specification. These devices can be SmartWatches, sensors, and fitness equipment if they are able to expose the Heart Rate Measurement via Bluetooth. A list of already tested devices can be found here.
💡*Standard Heart Rate Devices, refers to devices with Heart Rate Measurement capability and Bluetooth exposure of the measurements according to Heart Rate profile specification. These devices can be SmartWatches, sensors, and fitness equipment if they are able to expose the Heart Rate Measurement via Bluetooth. A list of already tested devices can be found here.
💡Devices and sensors are interchangeable terms used throughout the documentation.
💡Bluetooth Manager is the RMSensorManager class.

Bluetooth Manager description

The Bluetooth Manager is intended to abstract the connection and data acquisition to Bluetooth Heart Rate devices, the functionalities handled by the SDK are:

  • Start/stop discovery of nearby devices.
  • Connect / Disconnect to device.
  • Listen/Callback for Sensor connection/disconnection.
  • Get heart rate measurements notifications.
  • Get steps notifications (only for Rook sensors).
  • Get Battery level
💡Devices discovery filters to show only those with the (180D) Bluetooth service of heart rate measurement

The Bluetooth Manager is later used by the RM Training class with predefined flows like

  1. Discovering devices
  2. Connecting to selected device
  3. Enabling notifications
  4. Listen for new data

But the developer can use Bluetooth Manager to create their own flow to fit own needs, for example:

  • Discover and connect to one device each hour, gather data and disconnect
  • Keep connected to the device all the day and enable/disable notifications at certain period of time

Bluetooth Manager RMSensorManager class

final public class RMSensorManager
ReturnsFunction
voidstartDiscovery()
voidstopDiscovery()
voidconnect(device: CBPeripheral)
voiddisconnect(device: CBPeripheral)
Int?connectionStatus()
voidenableStepsReading()

startDiscovery()

public func startDiscovery()

Starts the devices discovering, this is performed repeatedly until the stopDiscovery method is called. Each time a device is found it's appended to a list that can be obtained with the onSensorDiscovered callback method.

💡Devices are filtered to only those with the (180D) Bluetooth service of heart rate measurement

Throws:

  • Permission errors
  • Errors when activating sensors
  • Errors when reading properties of a Sensor like a Sensor's name.
Example
// Discover sensors
let sensorManager = RMSensorManager.shared

sensorManager.startDiscovery()

stopDiscovery()

public func stopDiscovery()

Stops the devices discovering.

Example
let sensorManager  = RMSensorManager.shared

sensorManager.startDiscovery()
// Some process
sensorManager.stopDiscovery()

connect()

public func connect(device : CBPeripheral)

Makes a connection to the selected device as CBPeripheral which was previously discovered by the Scanner. Calling this method also stops the discovery process.

Example
// Connect a Sensor
let sensorManager = RMSensorManager.shared
let sensor = CBPeripheral

sensorManager.connect(device: sensor)

connectionStatus()

public func connectionStatus()

Retrieves the Bluetooth connection status with the device.

Status list:

  • Status 0: case disconnected: The device is disconnected.
  • Status 1: case connecting: The device is connecting.
  • Status 2: case connected: The device is connected.
  • Status 3: case disconnecting: The device is disconnecting.
Example
// Connection status of the sensor
let sensorManager = RMSensorManager.shared
let sensorStatus = sensorManager.connectionStatus()
print("connection status \(sensorStatus)")

disconnect()

public func disconnect(device: CBPeripheral) throws

Drops the connection with the device.

Example

// Connect a sensor
let sensorManager = RMSensorManager.shared
let sensor = CBPeripheral

sensor.disconnect(device: sensor)

Callbacks

The following callbacks are used to receive updates from sensor activity.

RMSensorConnectionCallback

Provides the data related to the connection status and results.

public protocol RMSensorConnectionCallback
FunctionDescription
onSensorConnected(rmSensor: CBPeripheral)Called when a connection has been established.
onSensorDisconnected(peripheral: CBPeripheral)Called when the manager has disconnected from the device.
Example

class CustomClass: RMSensorConnectionCallback{
let sensorManager = RMSensorManager.shared

let sensor = CBPeripheral

override func viewDidLoad() {
sensorManager.connectionDelegate = self
sensorManager.connect(device: sensor)
sensorManager.disconnect(device : sensor)
}
func onSensorConnected(rmSensor: CBPeripheral) {
print(rmSensor)
}

func onSensorDisconnected(peripheral: CBPeripheral) {
print(peripheral)
}

}

RMSensorDiscoveryCallback

A set of methods that provide an array of discovered sensors.

public protocol RMSensorDiscoveryCallback
FunctionDescription
onSensorDiscovered(sensorList: [CBPeripheral])Callback when a sensor has been discovered and return an array of the discovered devices.
onDiscoveryStopped(sensorList: [CBPeripheral])Callback when the manager has stopped and return an array of the discovered sensors.
Example

class CustomClass: RMSensorDiscoveryCallback{
let sensorManager = RMSensorManager.shared

let sensor = CBPeripheral
override func viewDidLoad() {
sensorManager.dicoveryDelegate = self
sensorManager.startDiscovery()
sensorManager.stopDiscovery()
}

func onSensorDiscovered(sensorList: [CBPeripheral]){
print(sensorList)
}

func onDiscoveryStopped(sensorList: [CBPeripheral]){
print(sensorList)
}

}

RMSensorCallback

A set of methods that provide the data related to the sensor information

public protocol RMSensorCallback
FunctionDescription
func onHeartRateReceived(bpm: Int, badContact: Bool)Tells the callback the bpm data or if there is bad band contact.
onStepsReceived(steps: Int, cadence: Float, deltaSteps: Int)Tells the callback that the step data has been changed
onBatteryReceived(batteryLevel : Int)Tells the callback the battery level of the sensor.
Example

class CustomClass: ViewController, RMSensorConnectionCallback {
let sensorManager = RMSensorManager.shared

let sensor = CBPeripheral


override func viewDidLoad {
sensorManager.sensorDelegate = self
sensorManager.connect(device: sensor)
}

func onHeartRateReceived(bpm: Int, badContact: Bool{
print(bpm)
print(badContact)
}

func onStepsReceived(steps: Int, cadence: Float, deltaSteps: Int) {
print(steps)
print(cadence)
print(deltaSteps)
}

func onBatteryReceived(batteryLevel : Int) {
print(batteryLevel)
}
}