# react-native-wear

> React Native Module for interacting with Wear OS via React Native

Latest version **1.1.0** (published 2024-03-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-wear
pnpm add react-native-wear
yarn add react-native-wear
bun add react-native-wear
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2024-03-06 |
| First published | 2023-12-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 18.0.0 |
| Dependencies | 0 |
| Unpacked size | 52.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 171 |
| Maintainers | turtlepaw |
| Keywords | react-native, ios, android |

## Links

- npm: https://www.npmjs.com/package/react-native-wear
- Repository: https://github.com/fabOnReact/react-native-wear-connectivity
- Homepage: https://github.com/beaverfy/react-native-wear#readme
- Issues: https://github.com/beaverfy/react-native-wear/issues
- npm.io page: https://npm.io/package/react-native-wear

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2024-03-06
- 1.0.11 — 2023-12-29
- 1.0.10 — 2023-12-29
- 1.0.9 — 2023-12-28
- 1.0.7 — 2023-12-28
- 1.0.6 — 2023-12-28
- 1.0.5 — 2023-12-28
- 1.0.4 — 2023-12-28
- 1.0.3 — 2023-12-27

## README

# react-native-wear-connectivity

- Create a [Wear OS][1] app using react-native
- Connect two react-native apps (Wear OS and Android mobile app)
- **Both apps are written in react-native**

https://github.com/fabOnReact/react-native-wear-connectivity/assets/24992535/415fab47-7d76-4c72-80b9-c0d19ec25a49

**Note**: This library allows you to write your Android WearOS and Mobile apps in React Native, refer to [react-native-watch-connectivity][2] for Apple Watch development.

[1]: https://wearos.google.com
[2]: https://github.com/mtford90/react-native-watch-connectivity

# Table of Contents

- [Installation](#installation)
- [Example of implementation](#example-of-implementation)
- [How to create a WearOS app using react-native](#how-to-create-a-wearos-app-using-react-native)
- [API Documentation](#api-documentation)
- [Contributing](#contributing)

## Installation

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

or

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

## Example of implementation

Implementation of the above counter application.

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

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>
      <Text>{count}</Text>
      <Button title="increase counter" onPress={sendMessageToWear} />
    </View>
  );
}
```

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

[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

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

```
WearConnectivityModule failed to retrieve nodes with error:
java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 17: API: Wearable.API is not available on this device.
Connection failed with: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
```

The Android Phone did not install the Wear OS app and did not pair with Wear OS device. Follow this [instructions][21].

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

---
_Source: https://npm.io/package/react-native-wear · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
