Skip to content

Commit c40c1f8

Browse files
authored
Update CONTRIBUTING.md
1 parent 2f93662 commit c40c1f8

File tree

1 file changed

+85
-50
lines changed

1 file changed

+85
-50
lines changed

CONTRIBUTING.md

Lines changed: 85 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,109 @@ Contributions are always welcome, no matter how large or small!
44

55
We want this community to be friendly and respectful to each other. Please follow it in all your interactions with the project. Before contributing, please read the [code of conduct](./CODE_OF_CONDUCT.md).
66

7-
## Development workflow
7+
https://github.com/user-attachments/assets/100ee026-550f-4d84-b180-58874ae2b395
88

9-
This project is a monorepo managed using [Yarn workspaces](https://yarnpkg.com/features/workspaces). It contains the following packages:
9+
### How to run the React Native Mobile example
1010

11-
- The library package in the root directory.
12-
- The mobile android app is in the `example/` directory.
13-
- The Wear OS android app is in the `watch-example/` directory.
11+
You need to clone the `react-native-wear-connectivity` project, build and run the mobile app example.
1412

15-
To get started with the project, run `yarn` in the root directory to install the required dependencies for each package:
16-
17-
```sh
13+
```
14+
git clone https://github.com/fabOnReact/react-native-wear-connectivity
15+
cd react-native-wear-connectivity
16+
yarn
17+
cd example
1818
yarn
19+
yarn android
1920
```
2021

21-
### Pair the Android and WearOS emulators
22+
### How to run the Jetpack Compose WearOS example
2223

23-
- Create a new emulator of type [WearOS Large round][22].
24-
- Pair the Android emulator with the Wear OS emulator. Follow this [instructions][21].
24+
1) Clone the WearOS Jetpack Compose [example](https://github.com/fabOnReact/wearos-communication-with-rn)
2525

26-
### Build the Mobile App
26+
```
27+
git clone https://github.com/fabOnReact/wearos-communication-with-rn
28+
```
2729

28-
The first step is building the react-native Android mobile app.
30+
2) Open the project with android studio, build and run it on an [Android WearOS emulator](https://github-production-user-asset-6210df.s3.amazonaws.com/24992535/303911079-f6cb9f84-dc50-492b-963d-6d9e9396f451.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAVCODYLSA53PQK4ZA%2F20250125%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250125T110158Z&X-Amz-Expires=300&X-Amz-Signature=4bd2be95943124fe34fb13e6a54e9a2fe8a9c06d1eb8afdf005ce02cf43c90d1&X-Amz-SignedHeaders=host).
2931

30-
```bash
31-
cd example
32-
yarn install
33-
yarn start
34-
# Now build android
35-
yarn android
32+
3) Now you can pair the WearOS emulator with the Android Mobile Emulator as explained in these [instructions](https://developer.android.com/training/wearables/get-started/connect-phone).
33+
34+
### Detailed explanation on how WearOS (Jetpack Compose) communicates with React Native (Android Mobile)
35+
36+
**Sending messages from Jetpack Compose WearOS to React Native Mobile Device**
37+
38+
[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.
39+
40+
```kotlin
41+
fun sendMessageToClient(node: Node) {
42+
val jsonObject = JSONObject().apply {
43+
put("event", "message")
44+
put("text", "hello")
45+
}
46+
try {
47+
val sendTask = Wearable.getMessageClient(applicationContext).sendMessage(
48+
node.getId(), jsonObject.toString(), null
49+
)
50+
} catch (e: Exception) {
51+
Log.w("WearOS: ", "e $e")
52+
}
53+
}
3654
```
3755

38-
### Build the WearOS App
56+
The WearOS `sendMessageToClient` function retrieves the devices connected via bluetooth to the WearOS device, and sends a JSON payload to those devices.
57+
58+
The payload is:
3959

40-
```bash
41-
cd watch-example
42-
yarn install
43-
yarn start --port=8082
60+
```javascript
61+
{
62+
event: "message",
63+
text: "this is the message parameter",
64+
}
4465
```
4566

46-
Build the project with `yarn android`, open the [react native dev menu][23] and change the bundle location to `your-ip:8082` (for ex. `192.168.18.2:8082`).
47-
Now you can build the WearOS app with the command:
67+
The React Native Mobile App uses `watchEvents.on(eventName, callback)` to listen to the `message` event and to increase the number displayed in the React Native Mobile App. The implementation in the React Native Mobile example is in [CounterScreen/index.android.tsx](https://github.com/fabOnReact/react-native-wear-connectivity/blob/2f936622422e197c22bef228b44eb24b46c878ae/example/src/CounterScreen/index.android.tsx#L14-L16).
4868

49-
```bash
50-
yarn android
69+
```javascript
70+
useEffect(() => {
71+
const unsubscribe = watchEvents.on('message', () => {
72+
setCount((prevCount) => prevCount + 1);
73+
});
74+
75+
76+
return () => {
77+
unsubscribe();
78+
};
79+
}, []);
80+
```
81+
82+
**Sending messages from React Native Mobile Device to Jetpack Compose WearOS**
83+
84+
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).
85+
86+
```javascript
87+
const sendMessageToWear = () => {
88+
setDisabled(true);
89+
const json = { text: 'hello' };
90+
sendMessage(json, onSuccess, onError);
91+
};
5192
```
5293

53-
[20]: https://reactnative.dev/docs/next/signed-apk-android
54-
[21]: https://developer.android.com/training/wearables/get-started/connect-phone
55-
[22]: https://gist.github.com/assets/24992535/f6cb9f84-dc50-492b-963d-6d9e9396f451 'wear os large round'
56-
[23]: https://reactnative.dev/docs/debugging
94+
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:
95+
96+
```kotlin
97+
override fun onMessageReceived(messageEvent: MessageEvent) {
98+
val jsonObject = JSONObject(messageEvent.path)
99+
val event = jsonObject.getString("event")
100+
if (event.equals("message")) {
101+
count = count + 1;
102+
}
103+
}
104+
```
105+
106+
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).
107+
108+
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 to rename package name, application id and change the signing key to pair that example with your React Native App.
109+
57110

58111
### Sending a pull request
59112

@@ -62,19 +115,10 @@ yarn android
62115
When you're sending a pull request:
63116

64117
- Prefer small pull requests focused on one change.
65-
- Verify that linters and tests are passing.
66118
- Review the documentation to make sure it looks good.
67119
- Follow the pull request template when opening a pull request.
68120
- For pull requests that change the API or implementation, discuss with maintainers first by opening an issue.
69121

70-
### Linting and tests
71-
72-
[ESLint](https://eslint.org/), [Prettier](https://prettier.io/), [TypeScript](https://www.typescriptlang.org/)
73-
74-
We use [TypeScript](https://www.typescriptlang.org/) for type checking, [ESLint](https://eslint.org/) with [Prettier](https://prettier.io/) for linting and formatting the code, and [Jest](https://jestjs.io/) for testing.
75-
76-
Our pre-commit hooks verify that the linter and tests pass when committing.
77-
78122
### Publishing to npm
79123

80124
We use [release-it](https://github.com/release-it/release-it) to make it easier to publish new versions. It handles common tasks like bumping version based on semver, creating tags and releases etc.
@@ -84,12 +128,3 @@ To publish new versions, run the following:
84128
```sh
85129
yarn release
86130
```
87-
88-
### Scripts
89-
90-
The `package.json` file contains various scripts for common tasks:
91-
92-
- `yarn`: setup project by installing dependencies.
93-
- `yarn typecheck`: type-check files with TypeScript.
94-
- `yarn lint`: lint files with ESLint.
95-
- `yarn test`: run unit tests with Jest.

0 commit comments

Comments
 (0)