Skip to content

Build Wear OS apps with react-native and connect them with other apps.

License

Notifications You must be signed in to change notification settings

fabOnReact/react-native-wear-connectivity

Repository files navigation

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
Screen.Recording.2024-02-12.at.4.21.58.PM.mov

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 with renative

The app generated with this implementation is available here.

Create a new renative app for android and wearos:

npx rnv new

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

cd YourFolder
yarn install

Run the app on the Android Emulator:

yarn rnv run -p android

Run the app on the WearOS Emulator:

yarn rnv run -p androidwear

Add the dependency react-native-wear-connectivity to your renative.json:

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

For more information refer to the official renative documentation and github repository.

Installation with React Native

yarn add react-native-wear-connectivity

or

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

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

Example of implementation

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

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

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

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 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 to learn how to contribute to the repository and the development workflow.

Feature requests are discussed in the issue tracker.

License

MIT