Menu

 

Native

Android library for integrating WeFitter and Samsung Health into your app

Installation

Add wefitter-shealth-1.5.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:

Show code example
Hide code example
dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar", "*.aar"])
    ...
    implementation "androidx.security:security-crypto:1.0.0"
    implementation "androidx.work:work-runtime-ktx:2.7.1"
    implementation "com.github.PhilJay:JWT:1.2.6"
    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:

Show code example
Hide code example

<pre>
...
import com.wefitter.shealth.WeFitterSHealth
import com.wefitter.shealth.WeFitterSHealthError

class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 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, &quot;configured: $configured&quot;)

                // enable connection if it hasn&#39;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 &amp;&amp; !weFitterSHealth.isConnected()) {
                    weFitterSHealth.connect()
                }
            }

            override fun onConnected(connected: Boolean) {
                Log.i(WeFitterSHealth.TAG, &quot;connected: $connected&quot;)
            }

            override fun onError(error: WeFitterSHealthError) {
                Log.e(WeFitterSHealth.TAG, &quot;error: ${error.reason.message}&quot;)

                // `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) { _, _ -&gt;
                            // `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 = &quot;CUSTOM_TITLE&quot;
            text = &quot;CUSTOM_TEXT&quot;
            iconResourceId = R.mipmap.ic_notification // pass custom icon resource id
            channelId = &quot;CUSTOM_CHANNEL_ID&quot;
            channelName = &quot;CUSTOM_CHANNEL_NAME&quot;
        }

        // 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(&quot;YOUR_TOKEN&quot;, &quot;YOUR_API_URL&quot;, statusListener, notificationConfig)
    }
}</pre>
 

You are subscribed!

You will soon receive our newsletter!
WeFitter team.
footer_bg