vishnu simha
vishnu simha

Reputation: 69

React Native for Wearables (Companion mode)

Can we build a React Native wearable Application?

Especially on companion mode.

Please provide any companion mode react native sample apps, if there are any.Thanks

Upvotes: 1

Views: 3064

Answers (2)

fabOnReact
fabOnReact

Reputation: 5942

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

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

Installation

yarn add react-native-wear-connectivity

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);
});

Upvotes: 0

Nooruddin Lakhani
Nooruddin Lakhani

Reputation: 6967

Currently React Native not officially support Wearables but you can use React native vanilla

Upvotes: 2

Related Questions