react-native-wear
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

react-native-wear-connectivity

  • Create a Wear OS 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 for Apple Watch development.

Table of Contents

Installation

yarn add react-native-wear

or

npm install react-native-wear

Example of implementation

Implementation of the above counter application.

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).
npx react-native@latest init YourMobileAppName
  • Add the following line to the new project AndroidManifest (file ):
<!-- 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.
  • Pair the Android emulator with the Wear OS emulator. Follow this instructions.
  • Start the metro server on port 8082 with yarn start --port=8082
  • Build the project with yarn android, open the react native dev menu 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)

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

API Documentation

Send Messages

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

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

Receive Messages

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.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

Feature requests are discussed in the issue tracker.

License

MIT

Package Sidebar

Install

npm i react-native-wear

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

53.7 kB

Total Files

75

Last publish

Collaborators

  • turtlepaw