Native
Android library for integrating WeFitter for Android into your app
Installation
Add wefitter-android-0.0.3.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.5.0"
implementation "androidx.room:room-runtime:2.3.0"
implementation "com.github.PhilJay:JWT:1.1.5"
implementation "com.google.code.gson:gson:2.8.7"
implementation "com.squareup.okhttp3:okhttp:3.12.12"
}
Usage
Add the following to your MainActivity.kt
:
import com.wefitter.android.WeFitter
const val ACTIVITY_RECOGNITION_REQUEST_CODE = 20
class MainActivity : Activity() {
// create instance of WeFitter
private val weFitter by lazy { WeFitter(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// check if the phone supports counting steps
if (weFitter.isSupported()) {
// create statusListener to receive status changes and errors - optional
val statusListener = object : WeFitter.StatusListener {
override fun onConfigured(configured: Boolean) {
Log.i(WeFitter.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 `weFitter.disconnect()` is also useful.
if (configured && !weFitter.isConnected()) {
// connect if permission has been granted, otherwise request it first
checkPermissionAndConnect()
}
}
override fun onConnected(connected: Boolean) {
Log.i(WeFitter.TAG, "connected: $connected")
}
override fun onError(error: String) {
Log.e(WeFitter.TAG, "error: $error")
}
}
// customize notification - optional
val notificationConfig = WeFitter.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 WeFitter
// 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/
weFitter.configure("YOUR_TOKEN", "YOUR_API_URL", statusListener, notificationConfig)
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
// connect if requested permission has been granted
if (requestCode == ACTIVITY_RECOGNITION_REQUEST_CODE && grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
weFitter.connect()
}
}
private fun checkPermissionAndConnect() {
// On 29+ a runtime permission needs to be requested before connecting
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// request permission if it has not been granted
if (checkSelfPermission(Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(
arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),
ACTIVITY_RECOGNITION_REQUEST_CODE
)
}
} else {
weFitter.connect()
}
}
}