-
Notifications
You must be signed in to change notification settings - Fork 0
Add Wear OS counter screen with Wearable sync #16
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
Open
fabOnReact
wants to merge
1
commit into
main
Choose a base branch
from
codex/create-counterscreen-with-sync-features
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/wearconnectivityexample/ui/screens/CounterScreen.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.wearconnectivityexample.ui.screens | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.unit.dp | ||
import androidx.wear.compose.material.Button | ||
import androidx.wear.compose.material.Text | ||
import com.google.android.gms.wearable.MessageClient | ||
import com.google.android.gms.wearable.Wearable | ||
import org.json.JSONObject | ||
|
||
@Composable | ||
fun CounterScreen() { | ||
val context = LocalContext.current | ||
var count by remember { mutableStateOf(0) } | ||
val messageClient = remember { Wearable.getMessageClient(context) } | ||
val nodeClient = remember { Wearable.getNodeClient(context) } | ||
|
||
fun sendCounter(value: Int) { | ||
val json = JSONObject().apply { | ||
put("event", "counter") | ||
put("value", value) | ||
} | ||
nodeClient.connectedNodes.addOnSuccessListener { nodes -> | ||
nodes.forEach { node -> | ||
messageClient.sendMessage(node.id, json.toString(), null) | ||
} | ||
} | ||
} | ||
|
||
DisposableEffect(Unit) { | ||
val listener = MessageClient.OnMessageReceivedListener { event -> | ||
try { | ||
val json = JSONObject(event.path) | ||
if (json.optString("event") == "counter") { | ||
count = json.optInt("value", count) | ||
} | ||
} catch (_: Exception) { | ||
} | ||
} | ||
messageClient.addListener(listener) | ||
onDispose { messageClient.removeListener(listener) } | ||
} | ||
|
||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Text(text = count.toString()) | ||
Spacer(modifier = Modifier.height(8.dp)) | ||
Button(onClick = { | ||
count += 1 | ||
sendCounter(count) | ||
}) { | ||
Text("Increment") | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] SendMessage path violates Wear OS requirements
sendCounter
builds a JSON string and passes it as thepath
argument tomessageClient.sendMessage
. Wearable messages require the path to start with/
and only thedata
argument should contain payload bytes. Because the supplied path is something like{"event":"counter",...}
it will throwIllegalArgumentException
and never dispatch when the button is tapped, so the counter cannot sync. Use a valid path (e.g.,/counter
) and move the JSON intodata
.Useful? React with 👍 / 👎.