Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring, adding more logs #4

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
package com.wearconnectivity;

import static com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST;

import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.util.RNLog;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
Expand All @@ -37,7 +31,10 @@ public class WearConnectivityModule extends WearConnectivitySpec
super(context);
context.addLifecycleEventListener(this);
client = Wearable.getMessageClient(context);
Log.d(TAG, TAG + "onMessageReceived listener added when activity is created. Client receives messages.");
Log.d(
TAG,
TAG
+ "onMessageReceived listener added when activity is created. Client receives messages.");
client.addListener(this);
}

Expand All @@ -54,47 +51,49 @@ public void multiply(double a, double b, Promise promise) {
promise.resolve(a * b);
}

@ReactMethod
public void sendMessage(String path, Promise promise) {
private List<Node> retrieveNodes(Promise promise) {
try {
NodeClient nodeClient = Wearable.getNodeClient(getReactApplicationContext());
List<Node> nodes = Tasks.await(nodeClient.getConnectedNodes());
if (nodes.size() > 0) {
for (Node node : nodes) {
// TODO: Add check that node is listening (companion app activity is used)
// https://developers.google.com/android/reference/com/google/android/gms/wearable/Node
if (node.isNearby()) {
sendMessageToClient(path, node, promise);
}
}
} else {
promise.reject(TAG, TAG + "sendMessage failed. No connected nodes found.");
}
// TODO: implement Runnable to run task in the background thread
// https://stackoverflow.com/a/64969640/7295772
return Tasks.await(nodeClient.getConnectedNodes());
} catch (Exception e) {
promise.reject(TAG, TAG + "sendMessage failed with exception: " + e);
return null;
}
}

@ReactMethod
public void sendMessage(String path, Promise promise) {
List<Node> connectedNodes = retrieveNodes(promise);
if (connectedNodes != null && connectedNodes.size() > 0 && client != null) {
for (Node connectedNode : connectedNodes) {
if (connectedNode.isNearby()) {
sendMessageToClient(path, connectedNode, promise);
}
}
} else {
promise.reject(
TAG,
TAG
+ "sendMessage failed. No connected nodes found. client: "
+ client
+ " connectedNodes: "
+ connectedNodes);
}
}

private void sendMessageToClient(String path, Node node, Promise promise) {
try {
Task<Integer> sendTask =
Wearable.getMessageClient(getReactApplicationContext())
.sendMessage(node.getId(), path, null);
// the last parameter is for file transfer (for ex. audio)
Task<Integer> sendTask = client.sendMessage(node.getId(), path, null);
OnSuccessListener<Object> onSuccessListener =
new OnSuccessListener<Object>() {
@Override
public void onSuccess(Object object) {
object ->
promise.resolve(TAG + "message sent to client with nodeID: " + object.toString());
}
};
sendTask.addOnSuccessListener(onSuccessListener);
OnFailureListener onFailureListener =
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
promise.reject(TAG, TAG + "sendMessage failed: " + e);
}
};
object ->
promise.resolve(TAG + "message sent to client with nodeID: " + object.toString());
sendTask.addOnSuccessListener(onSuccessListener);
sendTask.addOnFailureListener(onFailureListener);
} catch (Exception e) {
promise.reject(TAG, TAG + "sendMessage failed: " + e);
Expand All @@ -116,20 +115,29 @@ private void sendEvent(
@Override
public void onHostResume() {
if (client != null) {
Log.d(TAG, TAG + "onMessageReceived listener added when activity is resumed. Client receives messages.");
Log.d(
TAG,
TAG
+ "onMessageReceived listener added when activity is resumed. Client receives messages.");
client.addListener(this);
}
}

@Override
public void onHostPause() {
Log.d(TAG, TAG + "onMessageReceived listener removed when the activity paused. Client does not receive messages.");
Log.d(
TAG,
TAG
+ "onMessageReceived listener removed when the activity paused. Client does not receive messages.");
client.removeListener(this);
}

@Override
public void onHostDestroy() {
Log.d(TAG, TAG + "onMessageReceived listener removed when activity is destroyed. Client does not receive messages.");
Log.d(
TAG,
TAG
+ "onMessageReceived listener removed when activity is destroyed. Client does not receive messages.");
client.removeListener(this);
}
}
10 changes: 6 additions & 4 deletions watch-example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@
};
}, []);

const sendMessageToPhone = async () => {
const result = await sendMessage(INCREASE_PHONE_COUNTER_EVENT);

Check warning on line 41 in watch-example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'result' is already declared in the upper scope on line 17 column 10
console.log(result);
};

return (
<View style={styles.container}>
<Text>count is: {count}</Text>
<TouchableOpacity
onPress={() => sendMessage(INCREASE_PHONE_COUNTER_EVENT)}
style={styles.button}
/>
<TouchableOpacity onPress={sendMessageToPhone} style={styles.button} />
</View>
);
}
Expand Down
Loading