From c860be34ee7e0e1d14ede04601b65f4ca933157c Mon Sep 17 00:00:00 2001 From: Fabrizio Bertoglio Date: Fri, 9 Feb 2024 12:22:40 +0800 Subject: [PATCH] adding logging --- .../wearconnectivity/WearConnectivityModule.java | 14 ++++++++++++-- example/src/WearScreen.tsx | 7 +++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/wearconnectivity/WearConnectivityModule.java b/android/src/main/java/com/wearconnectivity/WearConnectivityModule.java index 0f93321..3c5f58a 100644 --- a/android/src/main/java/com/wearconnectivity/WearConnectivityModule.java +++ b/android/src/main/java/com/wearconnectivity/WearConnectivityModule.java @@ -2,6 +2,7 @@ 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; @@ -14,6 +15,7 @@ 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; @@ -35,6 +37,7 @@ 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."); client.addListener(this); } @@ -58,7 +61,11 @@ public void sendMessage(String path, Promise promise) { List nodes = Tasks.await(nodeClient.getConnectedNodes()); if (nodes.size() > 0) { for (Node node : nodes) { - sendMessageToClient(path, node, promise); + // 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."); @@ -95,7 +102,7 @@ public void onFailure(@NonNull Exception e) { } public void onMessageReceived(MessageEvent messageEvent) { - FLog.d(TAG, " onMessageReceived called for path: " + messageEvent.getPath()); + Log.d(TAG, TAG + "onMessageReceived received message with path: " + messageEvent.getPath()); sendEvent(getReactApplicationContext(), messageEvent.getPath(), null); } @@ -109,17 +116,20 @@ private void sendEvent( @Override public void onHostResume() { if (client != null) { + 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."); client.removeListener(this); } @Override public void onHostDestroy() { + Log.d(TAG, TAG + "onMessageReceived listener removed when activity is destroyed. Client does not receive messages."); client.removeListener(this); } } diff --git a/example/src/WearScreen.tsx b/example/src/WearScreen.tsx index 46fb57a..f1ac67c 100644 --- a/example/src/WearScreen.tsx +++ b/example/src/WearScreen.tsx @@ -32,10 +32,9 @@ export default function WearCounter() { }; }, []); - const onPressHandler = () => { - const onFulfilled = (value) => console.log('sendMessage succed with value: ', value); - const onRejected = (error) => console.log('sendMessage failed with error: ', error); - sendMessage(INCREASE_WEAR_COUNTER_EVENT).then(onFulfilled, onRejected); + const onPressHandler = async () => { + const result = await sendMessage(INCREASE_WEAR_COUNTER_EVENT); + console.log(result); }; return (