@d4l/react-native-wear-connectivity
v0.2.1
Published
Enstablish a two-way connection with wearOS
Downloads
79
Readme
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
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
- Example of implementation
- How to create a WearOS app using react-native
- API Documentation
- Contributing
Installation
yarn add react-native-wear-connectivity
or
npm install react-native-wear-connectivity
Example of implementation
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>
);
}
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 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.
API Documentation
Send Messages
import {
sendMessage,
sendGenuineMessage,
} from 'react-native-wear-connectivity';
sendMessage({ text: 'Hello watch!' });
sendGenuineMessage('/action');
Receive Messages
import { watchEvents } from 'react-native-wear-connectivity';
const unsubscribe = watchEvents.on('message', (message) => {
console.log('received message from watch', message);
});
Get Nodes based on Capabilities
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)
);
Open URI remotely from the react-native app on the Android app
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)
);
});
}
);
FAQ
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.
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