- 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
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.
- Installation
- Example of implementation
- How to create a WearOS app using react-native
- API Documentation
- Contributing
yarn add react-native-wear-connectivity
or
npm install react-native-wear-connectivity
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-connectivity';
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);
sendGenuineMessage('/hello', onSuccess, onError);
};
return (
<View>
<Text>{count}</Text>
<Button title="increase counter" onPress={sendMessageToWear} />
</View>
);
}
- 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 toyour-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.
import {
sendMessage,
sendGenuineMessage,
} from 'react-native-wear-connectivity';
sendMessage({ text: 'Hello watch!' });
sendGenuineMessage('/action');
import { watchEvents } from 'react-native-wear-connectivity';
const unsubscribe = watchEvents.on('message', (message) => {
console.log('received message from watch', message);
});
On the react-native phone app:
import { getReachableNodes } from 'react-native-wear-connectivity';
type AndroidNode = {
displayName: String,
id: String,
};
getReachableNodes(
(reachableNodes: AndroidNode[]) => {
reachableNodes?.forEach(({ displayName, id }: AndroidNode) => {
// do what you need with reachable nodes
});
},
(error: String) =>
console.error('message sent and received with error: ', error)
);
On the watch app: res/values/wear.xml
<resources xmlns:tools="http://schemas.android.com/tools"
tools:keep="@array/android_wear_capabilities">
<string-array name="android_wear_capabilities">
<item>my_android_capability</item>
</string-array>
</resources>
On the react-native phone app:
import {
getCapableAndReachableNodes,
getNonCapableAndReachableNodes,
} from 'react-native-wear-connectivity';
type AndroidNode = {
displayName: String,
id: String,
};
// https://developer.android.com/training/wearables/data/messages#advertise-capabilities
getCapableAndReachableNodes(
'my_android_capability',
(capableNodes: AndroidNode[]) => {
capableNodes?.forEach(({ displayName, id }: AndroidNode) => {
// do what you need with non-capable nodes
});
},
(error: String) =>
console.error('message sent and received with error: ', error)
);
getNonCapableAndReachableNodes(
'my_android_capability',
(nonCapableNodes: AndroidNode[]) => {
nonCapableNodes?.forEach(({ displayName, id }: AndroidNode) => {
// do what you need with non-capable nodes
});
},
(error: String) =>
console.error('message sent and received with error: ', error)
);
import { openRemoteURI } from 'react-native-wear-connectivity';
getNonCapableAndReachableNodes(
'sensorhub_wear',
(nonCapableNodes: AndroidNode[]) => {
nonCapableNodes?.forEach(({ displayName, id }: AndroidNode) => {
openRemoteURI(
`http://play.google.com/store/apps/details?id=my.app`,
id,
() => {
console.info(
'Open PlayStore Intent sent succesfully to ',
displayName
);
},
(error: String) =>
console.error('message sent and received with error: ', error)
);
});
}
);
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.
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