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.
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.
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" />
yarn start --port=8082
yarn android
, open the react native dev menu and change the bundle location to your-ip:8082
(for ex. 192.168.18.2:8082
).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 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;
import { sendMessage } from 'react-native-wear-connectivity';
sendMessage({ text: 'Hello watch!' });
import { watchEvents } from 'react-native-wear-connectivity';
const unsubscribe = watchEvents.on('message', (message) => {
console.log('received message from watch', message);
});
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).
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.
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.
See the contributing guide to learn how to contribute to the repository and the development workflow.
Feature requests are discussed in the issue tracker.
MIT