fabOnReact / react-native-wear-connectivity

Build Wear OS apps with react-native and connect them with other apps.
MIT License
95 stars 3 forks source link
android react-native renative renative-wear wear-api wear-os wearable wearable-devices wearos

react-native-wear-connectivity

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

npx react-native@latest init YourMobileAppName
<!-- this file is located at android/app/src/main/AndroidManifest.xml -->
<uses-feature android:name="android.hardware.type.watch" />

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