Data transfer
To set up the bracelet and the Android SDK here are the main steps to implement:
- Pair and connect to the bracelet
- Set up the bracelet parameter
- Set up the automatic data transfer between the Bracelet to the Android SDK database
- Retrieve the data
The end-to-end set-up of an app is described in Recommended set-up.
Step 3: Set up the automatic data transfer between the Bracelet to the Android SDK database
Prerequisites:
- The SDK is initialized with your licence key (Getting started).
- The bracelet is paired (Pairing) and its parameters are set (Set up the bracelet parameter).
- Automatic initialization is enabled with
enableAutoInitialization(Bracelet Initialization).
The Android SDK has an automatic file transfer of the data from the bracelet to the SDK. You subscribe a bracelet once. From then on a job runs every minute while the bracelet is connected: it downloads the bracelet's files, stores the data in the SDK database, erases from the bracelet what it stored, and updates the summaries. Your app is told through the listeners of the manager, and reads the data from the repositories: see What you get and where it arrives.
From SDK 2.0.0 a download initializes the bracelet after connecting and before transferring, with the configuration you gave to enableAutoInitialization. See Bracelet Initialization. If automatic initialization is not enabled and the bracelet is not initialized on this connection, the download does not start; it is retried later.
DeviceManager in the sample app (DataStartFragment.kt) is a sample-app helper, not SDK API. The examples below use the SDK only.
Data transfer
Activation
To start the automatic syncing of the data on the bracelet:
import com.corsano.sdk.ble.BleSdk
import com.corsano.sdk.ble.command.request.GetHwIdRequest
import com.corsano.sdk.ble.command.request.GetPlanRequest
import com.corsano.sdk.data.DataSdk
import com.corsano.sdk.data.util.awaitCommandV2
val downloadManager = DataSdk.getInstance().getDownloadManager()
// address: the MAC address of the paired bracelet. In a coroutine, with the bracelet connected:
val bleDevice = BleSdk.getInstance().getDevice(address)
val hardwareId = bleDevice.awaitCommandV2(GetHwIdRequest()).getOrThrow()
val plan = bleDevice.awaitCommandV2(GetPlanRequest()).getOrThrow().devicePlan
// Start Automatic Syncing of the data on the bracelet.
downloadManager.subscribeDeviceForDownload(
address = address,
hardwareId = hardwareId,
plan = plan,
onSuccess = { result("Automatic file transfer enabled") },
onError = { error -> result("Error: ${error.message}") }
)
What subscribeDeviceForDownload does:
- It stores the subscription on the phone and starts listening to the bracelet's pings.
onSuccessmeans that the subscription is stored, not that a download ran. Both callbacks are called on the main thread. - Subscribe once. The subscription is kept across restarts of your app: at every start the SDK listens again by itself.
- One bracelet at a time. A new subscription replaces the previous one.
- The first download starts with the next ping of the bracelet, within about a minute when the bracelet is connected. To download at once, trigger a single download.
When a download runs:
- The bracelet sends a regular ping every minute. Each ping starts a download, in a background job (
WorkManager). Since 3.0.0 a ping starts no download within a minute of the start of the previous one, while the bracelet is pairing, or after the bracelet refused or did not answer the authentication. - If the bracelet is not connected, the job connects to it (with
keepAlive = true), then initializes it if needed. - Next to the pings, a background worker checks every 15 minutes whether the subscribed bracelet is connected, and connects to it if not. See
automaticReconnectIfNeededIntervalMinutes. - While a download runs, the SDK shows a progress notification ("Performing data download"). From Android 13 it is only shown when your app holds the
POST_NOTIFICATIONSpermission; the download runs without it too. - While the licence is locked no download starts. The downloads start again by themselves when a licence check unlocks the SDK.
LicenceError.NotConfirmedmeans that the phone has not reachedapi.health.cloud.corsano.comfor two days: give it a connection and callDataSdk.recheckLicence().LicenceError.NoLicenceKeymeans that your app does not include thedatamodule or removed its initialiser from the manifest; no download runs until it does. See Licence Key.
Which files are downloaded depends on the hardware and on your licence:
Any bracelet:
- Activity (heart rate, steps, SpO2, respiration, temperature, blood pressure)
- HRV
- Bracelet logs
- PPG, when your licence includes it
287-2B (HardwareId.BRACELET_287N_2B), in addition:
- Sleep
- When your licence includes them: PPG2 and Stress (EmoGraphy), ECG, BioZ, accelerometer, raw temperature
The table of licensed raw data and files is in The licence and raw data. Some raw data must also be switched on on the bracelet: see Enable raw streams.
Deactivate
To stop the automatic syncing of the data on the bracelet:
val downloadManager = DataSdk.getInstance().getDownloadManager()
downloadManager.unsubscribeDeviceForDownload(
address = address,
onSuccess = { result("Automatic file transfer disabled") },
onError = { error -> result("Error: ${error.message}") }
)
This removes the subscription and stops listening to the pings. A download that is running goes on to its end. When you unpair a bracelet, call stopAll(context, address, onSuccess, onError) instead: it also cancels a running download. See Pairing, Unpair.
Trigger a single download
We recommend using the automatic transfer only: the data is transferred every minute. If needed, here is a way to start a full download at once:
val downloadManager = DataSdk.getInstance().getDownloadManager()
downloadManager.triggerDownload(address, hardwareId, plan)
- The download runs in the same background job as the automatic one. It replaces a download of that bracelet that is still running or waiting.
- It is not held back by the one-minute rule of the pings.
- It needs no subscription.
triggerDownloadreturns at once and has no callback. Follow the download with the progress, and get the data through the listeners.- While the licence is locked it does nothing.
Trigger a single download with filters
It is possible to trigger a download without one or more files: filteredFiles is the list of files to leave out. Here is an example without the PPG file:
Available from SDK 0.8.8
val downloadManager = DataSdk.getInstance().getDownloadManager()
downloadManager.triggerDownload(address, hardwareId, plan, listOf(FileOnWatch.PPG))
To leave out the sleep file, pass FileOnWatch.SLEEP.
Data transfer event
Progress during a transfer
The data transfer can take a few minutes if the bracelet hasn't been connected in a few days.
To view the progress of the download:
val downloadManager = DataSdk.getInstance().getDownloadManager()
downloadManager.getProgressLiveData().observe(viewLifecycleOwner) { listOfWorkInfo ->
downloadManager.parseProgressLiveData(listOfWorkInfo)?.let { downloadProgressInfo ->
if (downloadProgressInfo.workState == WorkInfo.State.RUNNING) {
result(
String.format(
"Download in progress %s %.02f %%",
downloadProgressInfo.step?.name ?: "",
downloadProgressInfo.progress
)
)
} else {
result("Download ${downloadProgressInfo.workState} at ${Date()}")
}
}
}
getProgressLiveData() gives the WorkManager state of the download job. parseProgressLiveData turns it into this object, or null when there is no job yet:
DownloadProgress(val workState: WorkInfo.State, val progress: Float, val step: FileOnWatch?)
| Field | Meaning |
|---|---|
workState | State of the job: ENQUEUED (waiting to start, or waiting for a retry), RUNNING, SUCCEEDED, FAILED or CANCELLED |
progress | While RUNNING: 0 to 100, over all files of this download; -1 before the first progress report. 100 when SUCCEEDED, 0 in the other states |
step | While RUNNING: the file that is being downloaded, or null. null in the other states |
Good to know:
- The files are downloaded one after the other.
- On a 287-2B the sleep file is reported as
FileOnWatch.CORSANO_SLEEP. Since 3.0.0 it is downloaded and reported once per download (before, it appeared twice: asSLEEP, then asCORSANO_SLEEP). FileOnWatch.NIBPis new in 3.0.0. The automatic download does not fetch it. A KotlinwhenoverFileOnWatchwithout anelsebranch has to handle it.
What can go wrong:
| Situation | What happens |
|---|---|
| The bracelet cannot be initialized | The job goes back to ENQUEUED and is retried later |
| The connection is lost, and the bracelet is not connected and bonded again within 30 seconds when the transfer starts | The job ends as FAILED |
| The bracelet does not answer a command or a transfer | Since 3.0.0 each command and each transfer of a download waits at most 2 minutes. That file stops for this download; the other files go on |
| The SDK cannot store a part of a file | Since 3.0.0 that part is not erased from the bracelet, so it comes again with the next download. The other files go on, and the job ends as FAILED |
| A download has been running for more than 5 minutes when a ping arrives | The job is started again |
In all cases, data that was not stored stays on the bracelet, and the next download fetches it.
Full code sample in the sample app file:
DataStartFragment.kt