Native
Android library for integrating WeFitter and Samsung Health into your app
Installation
Add wefitter-shealth-2.0.0.aar
and samsung-health-data-1.5.0.aar
to libs
folder which should normally be in app/libs
.
Add the following dependencies to your app/build.gradle
:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.aar'])
implementation "androidx.security:security-crypto:1.0.0"
implementation "androidx.work:work-runtime-ktx:2.8.1"
implementation "com.auth0.android:jwtdecode:2.0.2"
implementation "com.google.code.gson:gson:2.8.9"
implementation "com.squareup.okhttp3:okhttp:4.9.2"
}
Usage
Add the following to your MainActivity.kt
:
import com.wefitter.shealth.WeFitterSHealth
import com.wefitter.shealth.WeFitterSHealthError
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// create instance of WeFitterSHealth
val weFitterSHealth = WeFitterSHealth(this)
// create statusListener to receive status changes and errors - optional
val statusListener = object : WeFitterSHealth.StatusListener {
override fun onConfigured(configured: Boolean) {
Log.i(WeFitterSHealth.TAG, "configured: $configured")
// enable connection if it hasn't already been enabled,
// or move this for example to a button press or toggle change
// and then `weFitterSHealth.disconnect()` is also useful.
if (configured && !weFitterSHealth.isConnected()) {
weFitterSHealth.connect()
}
}
override fun onConnected(connected: Boolean) {
Log.i(WeFitterSHealth.TAG, "connected: $connected")
}
override fun onError(error: WeFitterSHealthError) {
Log.e(WeFitterSHealth.TAG, "error: ${error.reason.message}")
// `forUser` boolean indicates the error requires user interaction
// `reason` can be checked to override specific messages
if (error.reason.forUser) {
AlertDialog.Builder(this@SamsungMainActivity)
.setMessage(error.reason.message)
.setPositiveButton(android.R.string.ok) { _, _ ->
// `tryToResolveError` will guide the user on fixing certain errors
// for example:
// - link to the app store to install or update Samsung Health
// - open Samsung Health to accept privacy policy
// - open Settings when Samsung Health needs to be enabled
weFitterSHealth.tryToResolveError(error)
}
.setNegativeButton(android.R.string.cancel, null)
.show()
}
}
}
// customize notification - optional
val notificationConfig = WeFitterSHealth.NotificationConfig().apply {
// override fields
title = "CUSTOM_TITLE"
text = "CUSTOM_TEXT"
iconResourceId = R.mipmap.ic_notification // pass custom icon resource id
channelId = "CUSTOM_CHANNEL_ID"
channelName = "CUSTOM_CHANNEL_NAME"
}
// configure WeFitterSHealth
// token should be jwt and contain an id corresponding to a WeFitter profile
// api url should be base without v1.3/ingest/ as the library will append this
// for example: https://api.wefitter.com/api/
weFitterSHealth.configure("YOUR_TOKEN", "YOUR_API_URL", statusListener, notificationConfig)
}
}