Skip to content

Commit 5c9fbd2

Browse files
authored
Update README.md
1 parent eb7a7ba commit 5c9fbd2

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

README.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ https://github.com/user-attachments/assets/100ee026-550f-4d84-b180-58874ae2b395
1313

1414
- [Installation](#installation)
1515
- [React Native API Documentation](#react-native-api-documentation)
16+
- [Jetpack Compose API Documentation](#jetpack-compose-api-documentation)
1617
- [How to run the example](#how-to-run-the-example)
1718
- [FAQ on Troubleshooting Errors](#faq-on-troubleshooting-errors)
1819
- [Contributing](#contributing)
@@ -49,11 +50,56 @@ const unsubscribe = watchEvents.on('message', (message) => {
4950
});
5051
```
5152

53+
## Jetpack Compose API Documentation
54+
55+
### Send Messages
56+
57+
```kotlin
58+
import androidx.activity.ComponentActivity
59+
import com.google.android.gms.wearable.MessageClient
60+
import com.google.android.gms.wearable.Wearable
61+
import org.json.JSONObject
62+
import com.google.android.gms.wearable.Node
63+
64+
class MainActivity : ComponentActivity(), MessageClient.OnMessageReceivedListener {
65+
fun sendMessageToClient(node: Node) {
66+
val jsonObject = JSONObject().apply {
67+
put("event", "message")
68+
put("text", "hello")
69+
}
70+
val sendTask = Wearable.getMessageClient(applicationContext).sendMessage(
71+
node.getId(), jsonObject.toString(), null
72+
)
73+
}
74+
}
75+
```
76+
77+
### Receive Messages
78+
79+
```kotlin
80+
import androidx.activity.ComponentActivity
81+
import com.google.android.gms.wearable.MessageClient
82+
import com.google.android.gms.wearable.MessageEvent
83+
import org.json.JSONObject
84+
import androidx.compose.runtime.mutableStateOf
85+
86+
class MainActivity : ComponentActivity(), MessageClient.OnMessageReceivedListener {
87+
var count by mutableStateOf(0)
88+
override fun onMessageReceived(messageEvent: MessageEvent) {
89+
val jsonObject = JSONObject(messageEvent.path)
90+
val event = jsonObject.getString("event")
91+
if (event.equals("message")) {
92+
count = count + 1;
93+
}
94+
}
95+
}
96+
```
97+
5298
# How to run the example
5399

54100
I suggest you to try to run the example before doing your own implementation. You can try to modify the WearOS example and connect it to your mobile app following this instructions.
55101

56-
### How to run the React Native Mobile App example
102+
**How to run the React Native Mobile App example**
57103

58104
You need to clone the `react-native-wear-connectivity` project, build and run the mobile app example.
59105

@@ -66,7 +112,7 @@ yarn
66112
yarn android
67113
```
68114

69-
### How to run the Jetpack Compose WearOS example
115+
**How to run the Jetpack Compose WearOS example**
70116

71117
1) Clone the WearOS Jetpack Compose [example](https://github.com/fabOnReact/wearos-communication-with-rn)
72118
```
@@ -94,8 +140,7 @@ In our example, the gradle configs set the singingConfigs to use the same file d
94140
**Android Mobile React Native app**
95141
- Make sure both apps are using the same key, in our example the singingConfigs for the React Native Mobile App are configured [here](https://github.com/fabOnReact/react-native-wear-connectivity/blob/2f936622422e197c22bef228b44eb24b46c878ae/example/android/app/build.gradle#L78-L104) and the [debug.keystore](https://github.com/fabOnReact/wearos-communication-with-rn/blob/371e6c5862d49ccbff08ab951a26284a216daf97/app/debug.keystore) is the same from the WearOS app.
96142

97-
### How to implement the Jetpack Compose WearOS app
98-
143+
### Detailed explanation of the Implementation
99144
**Sending messages from Jetpack Compose WearOS to React Native Mobile Device**
100145

101146
[sendMessageToClient](https://github.com/fabOnReact/wearos-communication-with-rn/blob/371e6c5862d49ccbff08ab951a26284a216daf97/app/src/main/java/com/wearconnectivityexample/presentation/MainActivity.kt#L75-L87) is implemented on Jetpack Compose WearOS to send messages to the React Native Mobile App. `sendMessageToClient` is triggered on WearOS when [clicking](https://github.com/fabOnReact/wearos-communication-with-rn/blob/371e6c5862d49ccbff08ab951a26284a216daf97/app/src/main/java/com/wearconnectivityexample/presentation/WearApp.kt#L31) on the watch Button Component.
@@ -142,6 +187,34 @@ useEffect(() => {
142187
}, []);
143188
```
144189

190+
**Sending messages from React Native Mobile Device to Jetpack Compose WearOS**
191+
192+
The React Native Mobile App Example sends messages to the WearOS Jetpack Compose example with [sendMessage](https://github.com/fabOnReact/react-native-wear-connectivity/blob/2f936622422e197c22bef228b44eb24b46c878ae/example/src/CounterScreen/index.android.tsx#L29-L33).
193+
194+
```javascript
195+
const sendMessageToWear = () => {
196+
setDisabled(true);
197+
const json = { text: 'hello' };
198+
sendMessage(json, onSuccess, onError);
199+
};
200+
```
201+
202+
The Jetpack Compose WearOS app implements [onMessageReceived](https://github.com/fabOnReact/wearos-communication-with-rn/blob/371e6c5862d49ccbff08ab951a26284a216daf97/app/src/main/java/com/wearconnectivityexample/presentation/MainActivity.kt#L89-L95) and updates the Counter number on the screen when the message is received:
203+
204+
```kotlin
205+
override fun onMessageReceived(messageEvent: MessageEvent) {
206+
val jsonObject = JSONObject(messageEvent.path)
207+
val event = jsonObject.getString("event")
208+
if (event.equals("message")) {
209+
count = count + 1;
210+
}
211+
}
212+
```
213+
214+
onMessageReceived modifies the [count state variable](https://github.com/fabOnReact/wearos-communication-with-rn/blob/371e6c5862d49ccbff08ab951a26284a216daf97/app/src/main/java/com/wearconnectivityexample/presentation/MainActivity.kt#L31) and re-renders the Counter component with a new [text](https://github.com/fabOnReact/wearos-communication-with-rn/blob/371e6c5862d49ccbff08ab951a26284a216daf97/app/src/main/java/com/wearconnectivityexample/presentation/WearApp.kt#L46).
215+
216+
You can copy the [implementation](https://github.com/fabOnReact/wearos-communication-with-rn/tree/main/app/src/main/java/com/wearconnectivityexample/presentation) from the example, or follow the [instructions above](https://github.com/fabOnReact/wearos-communication-with-rn?tab=readme-ov-file#both-apps-share-the-same-package-name-and-applicationid) to rename package name, application id and change the signing key to pair that example with your React Native App.
217+
145218
## FAQ on Troubleshooting Errors
146219

147220
While some error messages are displayed on the metro server for the mobile or wearOS device (port 8082), other warnings are only available through logcat.

0 commit comments

Comments
 (0)