Skip to content

Commit f2fff48

Browse files
authored
Adding back instructions for renative and react-native (#35)
1 parent 70afcf0 commit f2fff48

File tree

2 files changed

+234
-7
lines changed

2 files changed

+234
-7
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ https://github.com/user-attachments/assets/100ee026-550f-4d84-b180-58874ae2b395
1515
- [React Native API Documentation](#react-native-api-documentation)
1616
- [Jetpack Compose API Documentation](#jetpack-compose-api-documentation)
1717
- [How to run the example](#how-to-run-the-example)
18+
- [Alternative methods of installation](#alternative-methods-of-installation)
1819
- [FAQ on Troubleshooting Errors](#faq-on-troubleshooting-errors)
1920
- [Contributing](#contributing)
2021

@@ -111,7 +112,7 @@ You need to clone the `react-native-wear-connectivity` project, build and run th
111112

112113
```
113114
git clone https://github.com/fabOnReact/react-native-wear-connectivity
114-
cd react-native-wear-connectivity
115+
cd react-native-wear-connectivity
115116
yarn
116117
cd example
117118
yarn
@@ -120,12 +121,14 @@ yarn android
120121

121122
**How to run the Jetpack Compose WearOS example**
122123

123-
1) Clone the WearOS Jetpack Compose [example](https://github.com/fabOnReact/wearos-communication-with-rn)
124+
1. Clone the WearOS Jetpack Compose [example](https://github.com/fabOnReact/wearos-communication-with-rn)
125+
124126
```
125127
git clone https://github.com/fabOnReact/wearos-communication-with-rn
126128
```
127-
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).
128-
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).
129+
130+
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).
131+
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).
129132

130133
**Make sure you respect this requirements:**
131134

@@ -138,15 +141,18 @@ Generate the app using the same package name and applicationId of the React Nati
138141
Make sure both apps use the same signing key. You can verify it as follows:
139142

140143
**Jetpack Compose App WearOS app** (no react-native)
144+
141145
- Verify that your build.gradle.kts on WearOS uses the same certificate from the Mobile App. The WearOS example configurations are [here](https://github.com/fabOnReact/wearos-communication-with-rn/blob/371e6c5862d49ccbff08ab951a26284a216daf97/app/build.gradle.kts#L21-L38) for our WearOS Jetpack Compose example.
142-
- Make sure the two projects use the same keystore. The WearOS project uses the same [debug.keystore](https://github.com/fabOnReact/wearos-communication-with-rn/blob/main/app/debug.keystore) of the Mobile App.
146+
- Make sure the two projects use the same keystore. The WearOS project uses the same [debug.keystore](https://github.com/fabOnReact/wearos-communication-with-rn/blob/main/app/debug.keystore) of the Mobile App.
143147

144148
In our example, the gradle configs set the singingConfigs to use the same file debug.keystore from the React Native Mobile App. The same configuration needs to be done for the release/production key.
145149

146150
**Android Mobile React Native app**
151+
147152
- 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.
148153

149154
### Detailed explanation of the Implementation
155+
150156
**Sending messages from Jetpack Compose WearOS to React Native Mobile Device**
151157

152158
[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.
@@ -167,7 +173,7 @@ fun sendMessageToClient(node: Node) {
167173
}
168174
```
169175

170-
The WearOS `sendMessageToClient` function retrieves the devices connected via bluetooth to the WearOS device, and sends a JSON payload to those devices.
176+
The WearOS `sendMessageToClient` function retrieves the devices connected via bluetooth to the WearOS device, and sends a JSON payload to those devices.
171177

172178
The payload is:
173179

@@ -186,7 +192,6 @@ useEffect(() => {
186192
setCount((prevCount) => prevCount + 1);
187193
});
188194

189-
190195
return () => {
191196
unsubscribe();
192197
};
@@ -221,6 +226,10 @@ onMessageReceived modifies the [count state variable](https://github.com/fabOnRe
221226

222227
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.
223228

229+
## Alternative methods of installation
230+
231+
The instructions for writing the WearOS apps with react-native are available at [alternative-installation.md](docs/alternative-installation.md). React Native does not officially support WearOS, some essential components like CircularScrollView are not available in React Native. More info in Issues https://github.com/fabOnReact/react-native-wear-connectivity/issues/12 and https://github.com/andrew-levy/jetpack-compose-react-native/issues/9.
232+
224233
## FAQ on Troubleshooting Errors
225234

226235
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.

docs/alternative-installation.md

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
# Alternative installations
2+
3+
- [Installation with renative](#installation-with-renative)
4+
- [Installation with react-native](#installation-with-react-native)
5+
- [Example of implementation](#example-of-implementation)
6+
- [API Documentation](#api-documentation)
7+
- [FAQ on Troubleshooting Errors](#faq-on-troubleshooting-errors)
8+
- [Contributing](#contributing)
9+
10+
## Installation with renative
11+
12+
The app generated with this implementation is available [here](https://github.com/fabOnReact/react-native-wear-connectivity-renative-example).
13+
14+
Create a new renative app for android and wearos:
15+
16+
```sh
17+
npx rnv new
18+
```
19+
20+
Change folder to the newly created app and run yarn install:
21+
22+
```sh
23+
cd YourFolder
24+
yarn install
25+
```
26+
27+
Run the app on the Android Emulator:
28+
29+
```sh
30+
yarn rnv run -p android
31+
```
32+
33+
Run the app on the WearOS Emulator:
34+
35+
```sh
36+
yarn rnv run -p androidwear
37+
```
38+
39+
Add the dependency `react-native-wear-connectivity` to your [renative.json](https://github.com/fabOnReact/react-native-wear-connectivity-renative-example/blob/main/renative.json):
40+
41+
```json
42+
"plugins": {
43+
"react-native-wear-connectivity": {
44+
"version": "^0.1.9"
45+
}
46+
}
47+
```
48+
49+
- Pair the Android emulator with the Wear OS emulator ([instructions][21]).
50+
- Implement the [example](#example-of-implementation) in [src/app/index.tsx](https://github.com/fabOnReact/react-native-wear-connectivity-renative-example/blob/main/src/app/index.tsx).
51+
52+
For more information refer to the official renative [documentation](https://next.renative.org) and [github repository](https://github.com/flexn-io/renative).
53+
54+
## Installation with React Native
55+
56+
```sh
57+
yarn add react-native-wear-connectivity
58+
```
59+
60+
or
61+
62+
```sh
63+
npm install react-native-wear-connectivity
64+
```
65+
66+
This is a detailed explanation on how to create a WearOS app using react-native:
67+
68+
- Create a new react-native app using the same name as your Mobile app.
69+
It is important to use the same name because both apps need to share the same package name (AndroidManifest, build.gradle, the project files) and applicationId (build.gradle).
70+
71+
```sh
72+
npx react-native@latest init YourMobileAppName
73+
```
74+
75+
- Add the following line to the new project AndroidManifest (file ):
76+
77+
```xml
78+
<!-- this file is located at android/app/src/main/AndroidManifest.xml -->
79+
<uses-feature android:name="android.hardware.type.watch" />
80+
```
81+
82+
- Create a new emulator of type [WearOS Large round][22].
83+
- Pair the Android emulator with the Wear OS emulator. Follow this [instructions][21].
84+
- Start the metro server on port 8082 with `yarn start --port=8082`
85+
- 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`).
86+
- Repeat the same steps for the Android Phone Emulator and use a different port (for ex. 8081).
87+
- **Important Note**: Before publishing to Google Play, make sure that both apps are signed using the same key (instructions [here][20])
88+
89+
You can now build the app with `yarn android`. JS fast-refresh and the other metro functionalities work without problem.
90+
91+
You can find the instructions on how to build the example app for this project in the [CONTRIBUTING][43] section.
92+
93+
[43]: https://github.com/fabOnReact/react-native-wear-connectivity/blob/main/CONTRIBUTING.md
94+
[20]: https://reactnative.dev/docs/next/signed-apk-android
95+
[21]: https://developer.android.com/training/wearables/get-started/connect-phone
96+
[22]: https://gist.github.com/assets/24992535/f6cb9f84-dc50-492b-963d-6d9e9396f451 'wear os large round'
97+
[23]: https://reactnative.dev/docs/debugging
98+
99+
## Example of implementation
100+
101+
Example implementation of the above counter application for WearOS and Android Mobile.
102+
103+
```js
104+
import React, { useEffect, useState } from 'react';
105+
import { View, Text, Button, StyleSheet } from 'react-native';
106+
import { sendMessage, watchEvents } from 'react-native-wear-connectivity';
107+
108+
function App() {
109+
return <CounterScreen />;
110+
}
111+
112+
function CounterScreen() {
113+
const [count, setCount] = useState(0);
114+
115+
useEffect(() => {
116+
const unsubscribe = watchEvents.on('message', () => {
117+
setCount((prevCount) => prevCount + 1);
118+
});
119+
120+
return () => {
121+
unsubscribe();
122+
};
123+
}, []);
124+
125+
const onSuccess = (result) => console.log(result);
126+
const onError = (error) => console.log(error);
127+
128+
const sendMessageToWear = () => {
129+
const json = { text: 'hello' };
130+
sendMessage(json, onSuccess, onError);
131+
};
132+
133+
return (
134+
<View style={styles.container}>
135+
<Button title="increase counter" onPress={sendMessageToWear} />
136+
<Text style={styles.count}>The count is {count}</Text>
137+
</View>
138+
);
139+
}
140+
141+
const styles = StyleSheet.create({
142+
container: {
143+
flex: 1,
144+
alignItems: 'center',
145+
justifyContent: 'center',
146+
backgroundColor: '#FDFDFD',
147+
},
148+
count: {
149+
borderRadius: 3,
150+
padding: 5,
151+
backgroundColor: '#9C9A9D',
152+
textAlign: 'center',
153+
textAlignVertical: 'center',
154+
marginTop: 20,
155+
color: 'white',
156+
fontSize: 20,
157+
fontWeight: '500',
158+
},
159+
});
160+
161+
export default App;
162+
```
163+
164+
## API Documentation
165+
166+
### Send Messages
167+
168+
```js
169+
import { sendMessage } from 'react-native-wear-connectivity';
170+
171+
sendMessage({ text: 'Hello watch!' });
172+
```
173+
174+
### Receive Messages
175+
176+
```js
177+
import { watchEvents } from 'react-native-wear-connectivity';
178+
179+
const unsubscribe = watchEvents.on('message', (message) => {
180+
console.log('received message from watch', message);
181+
});
182+
```
183+
184+
## FAQ on Troubleshooting Errors
185+
186+
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.
187+
To display them you need to open the android logcat tool from within Android Studio, where you can select the emulator and filter the messages by package name (more info in this [screenshot][41]).
188+
189+
[41]: https://github.com/user-attachments/assets/87016f71-782d-4f28-88dc-2c5d013eae2f
190+
191+
#### Wearable App not installed on Mobile Device
192+
193+
The error displays on the Metro Server if the mobile device did not install the Wear App, which is used to pair mobile device with wearOS device.
194+
The Wear app is installed from Google Play and allows to pair the Wear Device with the Android Phone. Follow this [instructions][21] to pair WearOS emulator with Android Phone.
195+
196+
```
197+
The Android mobile phone needs to install the Google Play Wear app.
198+
```
199+
200+
#### wearOS device too far for bluetooth connection
201+
202+
Logcat (wearOS) shows the following log message when sending messages via bluetooth to a mobile device too far from the watch. The message is not displayed on the Metro Server.
203+
204+
```
205+
Pixel_8_Pro_API_35Device is too far for bluetooth connection.
206+
```
207+
208+
## Contributing
209+
210+
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
211+
212+
Feature requests are discussed in the [issue tracker][40].
213+
214+
[40]: https://github.com/fabOnReact/react-native-wear-connectivity/issues
215+
216+
## License
217+
218+
MIT

0 commit comments

Comments
 (0)