Skip to content

Commit

Permalink
Adding back instructions for renative and react-native (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabOnReact authored Jan 29, 2025
1 parent 70afcf0 commit f2fff48
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 7 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ https://github.com/user-attachments/assets/100ee026-550f-4d84-b180-58874ae2b395
- [React Native API Documentation](#react-native-api-documentation)
- [Jetpack Compose API Documentation](#jetpack-compose-api-documentation)
- [How to run the example](#how-to-run-the-example)
- [Alternative methods of installation](#alternative-methods-of-installation)
- [FAQ on Troubleshooting Errors](#faq-on-troubleshooting-errors)
- [Contributing](#contributing)

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

```
git clone https://github.com/fabOnReact/react-native-wear-connectivity
cd react-native-wear-connectivity
cd react-native-wear-connectivity
yarn
cd example
yarn
Expand All @@ -120,12 +121,14 @@ yarn android

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

1) Clone the WearOS Jetpack Compose [example](https://github.com/fabOnReact/wearos-communication-with-rn)
1. Clone the WearOS Jetpack Compose [example](https://github.com/fabOnReact/wearos-communication-with-rn)

```
git clone https://github.com/fabOnReact/wearos-communication-with-rn
```
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).
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).

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

**Make sure you respect this requirements:**

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

**Jetpack Compose App WearOS app** (no react-native)

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

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.

**Android Mobile React Native app**

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

### Detailed explanation of the Implementation

**Sending messages from Jetpack Compose WearOS to React Native Mobile Device**

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

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

The payload is:

Expand All @@ -186,7 +192,6 @@ useEffect(() => {
setCount((prevCount) => prevCount + 1);
});


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

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.

## Alternative methods of installation

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.

## FAQ on Troubleshooting Errors

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.
Expand Down
218 changes: 218 additions & 0 deletions docs/alternative-installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
# Alternative installations

- [Installation with renative](#installation-with-renative)
- [Installation with react-native](#installation-with-react-native)
- [Example of implementation](#example-of-implementation)
- [API Documentation](#api-documentation)
- [FAQ on Troubleshooting Errors](#faq-on-troubleshooting-errors)
- [Contributing](#contributing)

## Installation with renative

The app generated with this implementation is available [here](https://github.com/fabOnReact/react-native-wear-connectivity-renative-example).

Create a new renative app for android and wearos:

```sh
npx rnv new
```

Change folder to the newly created app and run yarn install:

```sh
cd YourFolder
yarn install
```

Run the app on the Android Emulator:

```sh
yarn rnv run -p android
```

Run the app on the WearOS Emulator:

```sh
yarn rnv run -p androidwear
```

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):

```json
"plugins": {
"react-native-wear-connectivity": {
"version": "^0.1.9"
}
}
```

- Pair the Android emulator with the Wear OS emulator ([instructions][21]).
- 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).

For more information refer to the official renative [documentation](https://next.renative.org) and [github repository](https://github.com/flexn-io/renative).

## Installation with React Native

```sh
yarn add react-native-wear-connectivity
```

or

```sh
npm install react-native-wear-connectivity
```

This is a detailed explanation on how to create a WearOS app using react-native:

- Create a new react-native app using the same name as your Mobile app.
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).

```sh
npx react-native@latest init YourMobileAppName
```

- Add the following line to the new project AndroidManifest (file ):

```xml
<!-- this file is located at android/app/src/main/AndroidManifest.xml -->
<uses-feature android:name="android.hardware.type.watch" />
```

- Create a new emulator of type [WearOS Large round][22].
- Pair the Android emulator with the Wear OS emulator. Follow this [instructions][21].
- Start the metro server on port 8082 with `yarn start --port=8082`
- 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`).
- Repeat the same steps for the Android Phone Emulator and use a different port (for ex. 8081).
- **Important Note**: Before publishing to Google Play, make sure that both apps are signed using the same key (instructions [here][20])

You can now build the app with `yarn android`. JS fast-refresh and the other metro functionalities work without problem.

You can find the instructions on how to build the example app for this project in the [CONTRIBUTING][43] section.

[43]: https://github.com/fabOnReact/react-native-wear-connectivity/blob/main/CONTRIBUTING.md
[20]: https://reactnative.dev/docs/next/signed-apk-android
[21]: https://developer.android.com/training/wearables/get-started/connect-phone
[22]: https://gist.github.com/assets/24992535/f6cb9f84-dc50-492b-963d-6d9e9396f451 'wear os large round'
[23]: https://reactnative.dev/docs/debugging

## Example of implementation

Example implementation of the above counter application for WearOS and Android Mobile.

```js
import React, { useEffect, useState } from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';
import { sendMessage, watchEvents } from 'react-native-wear-connectivity';

function App() {
return <CounterScreen />;
}

function CounterScreen() {
const [count, setCount] = useState(0);

useEffect(() => {
const unsubscribe = watchEvents.on('message', () => {
setCount((prevCount) => prevCount + 1);
});

return () => {
unsubscribe();
};
}, []);

const onSuccess = (result) => console.log(result);
const onError = (error) => console.log(error);

const sendMessageToWear = () => {
const json = { text: 'hello' };
sendMessage(json, onSuccess, onError);
};

return (
<View style={styles.container}>
<Button title="increase counter" onPress={sendMessageToWear} />
<Text style={styles.count}>The count is {count}</Text>
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: '#FDFDFD',
},
count: {
borderRadius: 3,
padding: 5,
backgroundColor: '#9C9A9D',
textAlign: 'center',
textAlignVertical: 'center',
marginTop: 20,
color: 'white',
fontSize: 20,
fontWeight: '500',
},
});

export default App;
```

## API Documentation

### Send Messages

```js
import { sendMessage } from 'react-native-wear-connectivity';

sendMessage({ text: 'Hello watch!' });
```

### Receive Messages

```js
import { watchEvents } from 'react-native-wear-connectivity';

const unsubscribe = watchEvents.on('message', (message) => {
console.log('received message from watch', message);
});
```

## FAQ on Troubleshooting Errors

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.
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]).

[41]: https://github.com/user-attachments/assets/87016f71-782d-4f28-88dc-2c5d013eae2f

#### Wearable App not installed on Mobile Device

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

```
The Android mobile phone needs to install the Google Play Wear app.
```

#### wearOS device too far for bluetooth connection

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.

```
Pixel_8_Pro_API_35Device is too far for bluetooth connection.
```

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.

Feature requests are discussed in the [issue tracker][40].

[40]: https://github.com/fabOnReact/react-native-wear-connectivity/issues

## License

MIT

0 comments on commit f2fff48

Please sign in to comment.