From e27b204d8a362a19a0f2741fef0fc2f323f6445a Mon Sep 17 00:00:00 2001 From: Fabrizio Bertoglio Date: Fri, 9 Feb 2024 12:24:04 +0800 Subject: [PATCH] adding logging and promise (#3) * resolve promise with relevant messages * adding logging --- .../WearConnectivityModule.java | 31 ++++++++++++------- example/src/App.tsx | 3 -- example/src/LoginScreen.tsx | 4 +-- example/src/WearScreen.tsx | 5 +-- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/android/src/main/java/com/wearconnectivity/WearConnectivityModule.java b/android/src/main/java/com/wearconnectivity/WearConnectivityModule.java index cdeedfb..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; @@ -28,13 +30,14 @@ public class WearConnectivityModule extends WearConnectivitySpec implements MessageClient.OnMessageReceivedListener, LifecycleEventListener { public static final String NAME = "WearConnectivity"; - private static final String TAG = "WearConnectivityModule"; + private static final String TAG = "WearConnectivityModule "; private final MessageClient client; WearConnectivityModule(ReactApplicationContext context) { 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); } @@ -51,7 +54,6 @@ public void multiply(double a, double b, Promise promise) { promise.resolve(a * b); } - // catch the ExecutionException @ReactMethod public void sendMessage(String path, Promise promise) { try { @@ -59,19 +61,21 @@ public void sendMessage(String path, Promise promise) { List nodes = Tasks.await(nodeClient.getConnectedNodes()); if (nodes.size() > 0) { for (Node node : nodes) { - sendMessageToClient(path, node); + // 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); + } } - promise.resolve(true); } else { - Toast.makeText(getReactApplicationContext(), "No connected nodes found", Toast.LENGTH_LONG) - .show(); + promise.reject(TAG, TAG + "sendMessage failed. No connected nodes found."); } } catch (Exception e) { - FLog.w(TAG, " getConnectedNodes raised Exception: " + e); + promise.reject(TAG, TAG + "sendMessage failed with exception: " + e); } } - private void sendMessageToClient(String path, Node node) { + private void sendMessageToClient(String path, Node node, Promise promise) { try { Task sendTask = Wearable.getMessageClient(getReactApplicationContext()) @@ -80,7 +84,7 @@ private void sendMessageToClient(String path, Node node) { new OnSuccessListener() { @Override public void onSuccess(Object object) { - FLog.d(TAG, " sendMessage called onSuccess for path: " + path); + promise.resolve(TAG + "message sent to client with nodeID: " + object.toString()); } }; sendTask.addOnSuccessListener(onSuccessListener); @@ -88,17 +92,17 @@ public void onSuccess(Object object) { new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { - FLog.d(TAG, " sendMessage called onFailure with error: " + e); + promise.reject(TAG, TAG + "sendMessage failed: " + e); } }; sendTask.addOnFailureListener(onFailureListener); } catch (Exception e) { - FLog.w(TAG, " sendMessage raised Exception: " + e); + promise.reject(TAG, TAG + "sendMessage failed: " + 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); } @@ -112,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/App.tsx b/example/src/App.tsx index 80501c1..cfea3ab 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -14,9 +14,6 @@ const App = () => { <> - - Result: {result} - ); }; diff --git a/example/src/LoginScreen.tsx b/example/src/LoginScreen.tsx index 9d105d4..3377e26 100644 --- a/example/src/LoginScreen.tsx +++ b/example/src/LoginScreen.tsx @@ -7,7 +7,7 @@ import { NativeModules, Button, } from 'react-native'; -import {GoogleSignin} from '@react-native-google-signin/google-signin'; +import { GoogleSignin } from '@react-native-google-signin/google-signin'; import Config from 'react-native-config'; export default function LoginScreen() { @@ -19,7 +19,7 @@ export default function LoginScreen() { const userInfo = await GoogleSignin.signIn(); console.log(JSON.stringify(userInfo)); } catch (error) { - console.log('ERROR IS: ' + JSON.stringify(error)); + console.log('GoogleSignin faile with error: ' + JSON.stringify(error)); } }; diff --git a/example/src/WearScreen.tsx b/example/src/WearScreen.tsx index 93f757e..f1ac67c 100644 --- a/example/src/WearScreen.tsx +++ b/example/src/WearScreen.tsx @@ -32,8 +32,9 @@ export default function WearCounter() { }; }, []); - const onPressHandler = () => { - sendMessage(INCREASE_WEAR_COUNTER_EVENT); + const onPressHandler = async () => { + const result = await sendMessage(INCREASE_WEAR_COUNTER_EVENT); + console.log(result); }; return (