Skip to main content

Set up

Here you will find developer guides for most important use-cases of the iOS SDK.

Requirements to setup before using the SDK

Summary slots interval

When initialising the SDK you can choose to have our default slots, or 1 minute slots

.sdkDefault .oneMinute

Note: if you decide to change this when the app has already been initialised, the change apply on the next summaries, which is always the next day. Or you will need to delete the app and reinstall the app.

Sample

private var sdk = CorsanoSDKObject(withCloudService: false)
sdk.startBLE()
sdk.startRealm(slotSettings: .sdkDefault)

Setting the bracelet plan

Bracelet plan

After connection starts with the BLE data will automatically be stored in the realm database (based on the bracelet plan). Please set the bracelet plan directly after the bracelet isBonded = true. See example below.

private func setBondedState(_ bonded: Bool) {
if bonded == true {
SDKManager.instance.getSDK().peripheralApi.setPlanDirectly(.hospital)

// more arguments available in this call like
// frequency - frequency of the PPG2 file stream - only V2 bracelet
// interval - interval of getting all activity files
// Example: SDKManager.instance.getSDK().peripheralApi.setPlanDirectly(.hospital_raw, frequency: .F128Hz, interval: 1)
}
}

Setting the user profile

Users profile Setting

It is important to set the user profile: (height, weight, wrist position, gender, metric unit, birthdate), because this will be send to the bracelet and be used in calculations. First get the current user from the database, then use the userProfileApi to set the right parameters. This will be synced to the bracelet on the background in a queue.

Sample code:

private var userProfileApi: UserProfileApi = SDKManager.instance.getSDK().userProfileApi
if let currentProfile = self.currentProfile {
userProfileApi.updateHeightInDb(currentProfile.getUUID(), value: self.selectedHeight) // (Int) height in cm
userProfileApi.updateWeightInDb(currentProfile.getUUID(), value: self.selectedWeight) // (Int) weight in kg
userProfileApi.updateWristInDb(currentProfile.getUUID(), value: ["Left", "Right"]) // (String) "Left" or "Right"
userProfileApi.updateGenderInDb(currentProfile.getUUID(), value: ["Male", "Female"]) // (String) "Male" or "Female"
userProfileApi.updateMetricUnitInDb(currentProfile.getUUID(), value: ["Metric", "Imperial"]) // (String) "Metric" or "Imperial"
userProfileApi.updateDateInDb(currentProfile.getUUID(), value: self.selectedDate) // (Date) user birthdate
}

Settings for sleep

Sleep - setting sleep bed and rise time

It is important to set the bracelet settings: (Schedule bedtime/risetime), because this will be send to the bracelet. This is the timeframe the bracelet will use to look for sleep and create a sleep summary. Please set this by using the userprofile.

Sample code:

private var appSettingsApi: AppSettingsApi = SDKManager.instance.getSDK().appSettingsApi
if let currentSettings = self.currentSettings {
userProfileApi.updateScheduleBedTime(79200) // (Int) Scheduled bedtime in sec. (ex 22:00:00 => 79200)
userProfileApi.updateScheduleRiseTime(25200) // (Int) Scheduled risetime in sec. (ex 07:00:00 => 25200)
}`

You can also call this functions to sync this parameters immediately with the bracelet (recommended after setting the sleep rise and bed time).

userProfileApi.sendBedTimeToBracelet()
userProfileApi.sendRiseTimeToBracelet()

Sleep - monitoring

Sleep monitoring will start from schedule Bedtime - 2h To Schedule RiseTime + 2h (ex: if your schedule bedtime/risetime is 22h00-7h00, the bracelet will start monitoring from 20h00 to 9h00). The sleep monitoring will stop automatically if the steps count reach 250 steps within a specific time range.

As soon as a sleep record has been processed by the sleep library, a sleep summary with sleep slots will be available.

You can retrieve a sleep summary by day or an array of the last seven sleep records from a date.

Sample code Get a sleep summary for a specific day and last seven sleep records:

private var sleepApi: MetricSleepAPI = SDKManager.instance.getSDK().sleepApi
sleepApi.getSleepSummeryFor(date) // (Date) date of the sleep
sleepApi.getLastSevenDaysSleepSummaryFrom(date) // (Date) get last seven sleep records from a date

Sleep Slots details

SleepTypeDefinition
0Undefined
1Awake sleep
2Light sleep
3Deep sleep
4REM sleep

Step by step guide

Scanning and pairing with bracelet iOS

Steps to scan and pair to the bracelet

  1. Start searching for the bracelet. This can be done by using the code example below. Please not that the bracelet should be connected to the charger during this time to prevent the bracelet from running out of battery during pairing.
let sdk = SDKManager.instance.getSDK()
sdk.bluetoothManager.startfindingBLE(pairing: true, withConnected: withConnected) { (didFindPeripherals, directConnect) in
if didFindPeripherals {
// proceed to the next page
self.foundBracelet = true
} else {
// show in UI that there is no bracelet found - check battery bracelet, connect to charger.
// or the bracelet is already connected to phone, please "forget device" via iOS settings.
self.timeOutCalled = true
}
}

There has been one or multiple bracelets found. Now we need to press the physical button of the bracelet which we want to connect. Please use the code example below to detect which bracelet button has been pressed. Please subscribe via the FoundPerpheral delegate.

let sdk = SDKManager.instance.getSDK()
sdk.delegates.buttonIsPressedDelegate = self
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500)) {
sdk.bluetoothManager.startLookingForPusherPressed()

DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(7)) {
withAnimation {
// will be called if the button is not pressed within 7 seconds. Show error in UI.
self.notPressedYet = true
}
}
}

// delegate function which will return when button is pressed
func buttonIsPressed(_ peripheralUUID: UUID) {
withAnimation {
// Button press succes, go to next screen.
self.showingAlert = true
}
}

After you get a callback in the buttonIsPressed delegate function, you can choose to show an alert with the pairing code between the phone and bracelet. This code is 999 999. After user is informed of this code you can proceed to the next step which is really setting the secured connection between phone and bracelet. See example code below.

let sdk = SDKManager.instance.getSDK()
sdk.bluetoothManager.connectToPeripheral()
After the previous step, you will get an confirmation from this delegate:
func peripheralConfirmed() {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) {
self.showLoader = false
self.deviceFound = true
}
}

This completes the pairing part of the bracelet to the phone.

Connecting to a bracelet on start

Automatically connect to the bracelet

This part of the documentation describes how the bracelet connects to the app after it has already been connected once. This happens automatically when the app starts using the BLE service from iOS.

If you have not connected the bracelet to the phone before nothing happens. Please check out the documentation on how to pair the bracelet to the phone first.

Please subscribe to the BLEBondingStateDelegate in your class. In the example app this can be found in the BluetoothStateViewModel. This subscription will automatically start all bluetooth services inside the SDK. After the bleManagerStateChanged (iOS native function) is called - and the bluetooth is on in the settings of the phone - we will call the checkIfCanReconnectOnStart function. See below.

fileprivate func checkIfCanReconnectOnStart() {
if !reconnectionChecked {
// this bool is to prevent the app of reconnecting every time.
// if you wish to force a reconnect, please use this function -> reconnect()
self.reconnectionChecked = true
SDKManager.instance.getSDK().bluetoothManager.checkIfPeripheralCanBeReconnected()
}
}

If the reconnection is successful - the bracelet will send all its data to the phone. This includes a battery-level, hardwareId, bondingState, braceletName, BraceletFWVersion etc. This function will automatically return these values every minute. For example: you can use this function to show the battery-level in the UI. Also use this function to check for the firmware version in the DFU service (see DFU section for more information). See below.

func braceletModelChanged(_ model: BraceletModel) {
self.bracelet = model
self.setBatteryLevel(model)
self.setHwId(model)
self.setBondedState(model.isBonded)
self.setActivityState(model)
self.braceletName = model.deviceName
self.braceletFWVersion = model.fwVersion
self.braceletSerialNumber = model.serialNumber
self.isInitialising = model.isInitialising
self.braceletPlatformId = String(model.hardwareKind?.rawValue ?? 0)
DFUUpdateViewModel.instance.checkFWStatus(fwString: braceletFWVersion, bondingState: isBonded, platformId: braceletPlatformId)
}
  • If the bracelet disconnects from the phone, the function bleDidDisconnect will be called. Please call reconnect() in this function. This will trigger a mechanisme in the SDK to try to reconnect to the bracelet every 2 seconds. See sample code in our sample project in BluetoothStateViewModel.

Disconnect (unpair) the bracelet from the app

This part of the documentation describes how to disconnect (unpair) the bracelet from the app.

  • First you have to inform the internal DB that you don't want to keep this bracelet connected to the app any more. By doing this operation, the app will not try to connect automaticaly to the bracelet anymore

Example:

SDKManager.instance.getSDK().peripheralApi.setPeripheralToInactive()
  • Then, you have to cancel the BLE connection with the BLE manager

Example:

SDKManager.instance.getSDK().bluetoothManager.disconnectFromPeripheral()
  • Finally, you have to inform the end user that he still have to remove (break) the secured bonding. This operation cannot be done programmatically. For security reasons Apple doesn't let developers making this operation programmaticaly. You have to prepare an UI to inform the user that he has to go in iOS general settings -> Bluetooth. Then click on the small "i" button in the bracelet tab and press on "Forget this device".

Getting bracelet status

Automatically connect to the bracelet

To the bracelet status please use this delegate from the SDK → BLEBondingStateDelegate

After successful pairing of the bracelet this delegate will return all known status details from the bracelet.

Example:

func braceletModelChanged(_ model: BraceletModel) {
self.bracelet = model
self.setBatteryLevel(model)
self.setHwId(model)
self.setBondedState(model.isBonded)
self.setActivityState(model)
self.braceletName = model.deviceName
self.braceletFWVersion = model.fwVersion
self.braceletSerialNumber = model.serialNumber
self.isInitialising = model.isInitialising
self.braceletPlatformId = String(model.hardwareKind?.rawValue ?? 0)
DFUUpdateViewModel.instance.checkFWStatus(fwString: braceletFWVersion, bondingState: isBonded, platformId: braceletPlatformId)
}
  • The call to the DFUUpdateViewModel is used to inject the firmware version of the bracelet to the DFU manager. This can be used to trigger a DFU from the app - and update the firmware on the bracelet via bluetooth.

Changing device plan

For the SDK it is important to only change the device mode after a secure pairing is enabled - on startup - between the bracelet and the phone.

Example:

fileprivate func setBondedState(_ bonded: Bool) {
LoggerService.shared.log("Braceletmodel isBonded: \(bonded)")
self.isBonded = bonded

if isBonded = true {
SDKManager.instance.getSDK().peripheralApi.setPlanDirectly(.hospital)

// EXAMPLE: For V2 bracelet to set 128Hz
// SDKManager.instance.getSDK().peripheralApi.setPlanDirectly(.hospital, frequency: .F128Hz, interval: 10)
}
}

Changing the modes in between after pairing

Changing the modes is always possible, only check if the bondedState == true.

Retrieving data

As we see in the previous section after when we set a certain plan - the bracelet wil automatically start sending certain data to the phone. This will be saved into our database. Please see the data models section for iOS for examples.

You can find the names and api’s for each metrics below:

MetricsBracelet VersionApi Name
- Heart rateV1 / V2MetricBPMApi
- Breathing rateV1 / V2MetricBreathApi
- StepsV1 / V2MetricStepsApi
- SpeedV1 / V2MetricStepsApi
- Energy expenditureV1 / V2MetricStepsApi
- Activity TypeV1 / V2MetricActivityApi
- Skin TemperatureV1 / V2MetricBodyTempApi
- Core body TemperatureONLY V2MetricCoreBodyTemperatureApi
- SaturationONLY V2MetricSpo2Api
- EDA (stress)ONLY V2MetricEmoghraphyApi
- SleepV1 / V2MetricSleepApi

Code Examples

Getting the bpm summary for a certain day:

SDKManager.instance.getSDK().bpmApi.getBPMSummeryFor(date) { (summery, change) in}

Getting the sleep summary for a certain day:

SDKManager.instance.getSDK().sleepApi.getSleepSummeryFor(date) { (summery, change) in}