Skip to content

Commit

Permalink
phone receives message from watch
Browse files Browse the repository at this point in the history
  • Loading branch information
fabOnReact committed Feb 8, 2024
1 parent 1567805 commit 60d99e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ public void onFailure(@NonNull Exception e) {

public void onMessageReceived(MessageEvent messageEvent) {
Log.w("TESTING: ", "from Phone onMessageReceived");
if (messageEvent.getPath().equals("increase_wear_counter")) {
sendEvent(getReactApplicationContext(), messageEvent.getPath(), null);
}
sendEvent(getReactApplicationContext(), messageEvent.getPath(), null);
}

private void sendEvent(
Expand Down
28 changes: 22 additions & 6 deletions watch-example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import React, {useEffect, useState} from 'react';
import React, { useEffect, useState } from 'react';

import { StyleSheet, View, Text, TouchableOpacity, NativeEventEmitter, NativeModules } from 'react-native';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
NativeEventEmitter,
NativeModules,
} from 'react-native';
import { multiply, sendMessage } from 'react-native-wear-connectivity';

const INCREASE_WEAR_COUNTER_EVENT = 'increase_wear_counter';
const INCREASE_PHONE_COUNTER_EVENT = 'increase_phone_counter';

export default function App() {
const [result, setResult] = useState<number | undefined>();

Check failure on line 17 in watch-example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'result' is assigned a value but never used. Allowed unused elements of array destructuring patterns must match /^_/u
const [count, setCount] = useState(0);
Expand All @@ -14,11 +23,14 @@ export default function App() {

useEffect(() => {
const eventEmitter = new NativeEventEmitter(
NativeModules.AndroidWearCommunication,
NativeModules.AndroidWearCommunication
);
let eventListener = eventEmitter.addListener(
INCREASE_WEAR_COUNTER_EVENT,
() => {
setCount((prevCount) => prevCount + 1);
}
);
let eventListener = eventEmitter.addListener(INCREASE_WEAR_COUNTER_EVENT, () => {
setCount(prevCount => prevCount + 1);
});

return () => {
eventListener.remove();
Expand All @@ -28,6 +40,10 @@ export default function App() {
return (
<View style={styles.container}>
<Text>count is: {count}</Text>
<TouchableOpacity
onPress={() => sendMessage(INCREASE_PHONE_COUNTER_EVENT)}
style={styles.button}
/>
</View>
);
}
Expand Down

0 comments on commit 60d99e0

Please sign in to comment.