Skip to content

Commit

Permalink
adding logging
Browse files Browse the repository at this point in the history
  • Loading branch information
fabOnReact committed Feb 9, 2024
1 parent 1d9f8b1 commit c860be3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -58,7 +61,11 @@ public void sendMessage(String path, Promise promise) {
List<Node> 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.");
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}
}
7 changes: 3 additions & 4 deletions example/src/WearScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit c860be3

Please sign in to comment.