Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes sending messages from the Android app to the Garmin watches #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Comm Android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ android {

dependencies {
implementation("androidx.recyclerview:recyclerview:1.2.1")
implementation("androidx.constraintlayout:constraintlayout:2.1.3")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.5.1")

implementation("com.garmin.connectiq:monkeybrains:1.0.2")
implementation(files("./libs/monkeybrains-sdk-release.aar"))
implementation("androidx.appcompat:appcompat:1.5.1")
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*/
package com.garmin.android.apps.connectiq.sample.comm.activities

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
Expand All @@ -14,6 +13,8 @@ import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.garmin.android.apps.connectiq.sample.comm.MessageFactory
Expand All @@ -24,6 +25,9 @@ import com.garmin.android.connectiq.IQApp
import com.garmin.android.connectiq.IQDevice
import com.garmin.android.connectiq.exception.InvalidStateException
import com.garmin.android.connectiq.exception.ServiceUnavailableException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

private const val TAG = "DeviceActivity"
private const val EXTRA_IQ_DEVICE = "IQDevice"
Expand All @@ -32,7 +36,7 @@ private const val COMM_WATCH_ID = "a3421feed289106a538cb9547ab12095"
// TODO Add a valid store app id.
private const val STORE_APP_ID = ""

class DeviceActivity : Activity() {
class DeviceActivity : AppCompatActivity() {

companion object {
fun getIntent(context: Context, device: IQDevice?): Intent {
Expand Down Expand Up @@ -209,18 +213,29 @@ class DeviceActivity : Activity() {
}

private fun onItemClick(message: Any) {
try {
connectIQ.sendMessage(device, myApp, message) { device, app, status ->
Toast.makeText(this@DeviceActivity, status.name, Toast.LENGTH_SHORT).show()
lifecycleScope.launch {
val (sendStatus, toastLength) = sendMessage(message)
Toast.makeText(this@DeviceActivity, sendStatus, toastLength).show()
}
}

suspend fun sendMessage(message: Any) : Pair<String, Int> {
var sendStatus = ""
var toastLength = Toast.LENGTH_SHORT

withContext(Dispatchers.IO) {
try {
connectIQ.sendMessage(device, myApp, message) { device, app, status ->
sendStatus = status.name
}
} catch (e: InvalidStateException) {
sendStatus = "ConnectIQ is not in a valid state"
} catch (e: ServiceUnavailableException) {
sendStatus = "ConnectIQ service is unavailable. Is Garmin Connect Mobile installed and running?"
toastLength = Toast.LENGTH_LONG
}
} catch (e: InvalidStateException) {
Toast.makeText(this, "ConnectIQ is not in a valid state", Toast.LENGTH_SHORT).show()
} catch (e: ServiceUnavailableException) {
Toast.makeText(
this,
"ConnectIQ service is unavailable. Is Garmin Connect Mobile installed and running?",
Toast.LENGTH_LONG
).show()
}

return Pair(sendStatus, toastLength)
}
}
2 changes: 1 addition & 1 deletion Comm Android/app/src/main/res/values-v11/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!-- API 11 theme customizations can go here. -->
</style>

Expand Down
2 changes: 1 addition & 1 deletion Comm Android/app/src/main/res/values-v14/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- API 14 theme customizations can go here. -->
</style>

Expand Down
2 changes: 1 addition & 1 deletion Comm Android/app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
Expand Down
4 changes: 2 additions & 2 deletions Comm Android/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
# Android SDK.
compileSdkVersion=30
compileSdkVersion=32
minSdkVersion=21
targetSdkVersion=30
targetSdkVersion=32