Android Open Source - rock-paper-scissors Watch Connection






From Project

Back to project page rock-paper-scissors.

License

The source code is released under:

MIT License

If you think the Android project rock-paper-scissors listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package au.com.codeka.rps;
/*  w w  w. j a  va  2s. com*/
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;

import java.util.ArrayList;

/**
 * Class that represents our connection to the watch.
 */
public class WatchConnection {
    private static final String TAG = "WatchConnection";
    private GoogleApiClient googleApiClient;
    private boolean isConnected;
    private ArrayList<Message> pendingMessages = new ArrayList<Message>();
    private ArrayList<String> watchNodes = new ArrayList<String>();

    public void setup(Context context) {
        googleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle connectionHint) {
                        Log.d(TAG, "onConnected: " + connectionHint);
                        Wearable.NodeApi.getConnectedNodes(googleApiClient).setResultCallback(
                                new ResultCallback<NodeApi.GetConnectedNodesResult>() {
                            @Override
                            public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
                                for (Node node : getConnectedNodesResult.getNodes()) {
                                    watchNodes.add(node.getId());
                                }
                                isConnected = true;
                                for (Message msg : pendingMessages) {
                                    sendMessage(msg);
                                }
                                pendingMessages.clear();
                            }
                        });
                    }
                    @Override
                    public void onConnectionSuspended(int cause) {
                        Log.d(TAG, "onConnectionSuspended: " + cause);
                        isConnected = false;
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult result) {
                        Log.d(TAG, "onConnectionFailed: " + result);
                        isConnected = false;
                    }
                })
                .addApi(Wearable.API)
                .build();
    }

    public void start() {
        googleApiClient.connect();
    }

    public void stop() {
        googleApiClient.disconnect();
    }

    public void sendMessage(Message msg) {
        if (!isConnected) {
            pendingMessages.add(msg);
        } else {
            for (String watchNode : watchNodes) {
                Wearable.MessageApi.sendMessage(googleApiClient, watchNode, msg.getPath(),
                        msg.getPayload());
            }
        }
    }

    public static class Message {
        private final String path;
        private final byte[] payload;

        public Message(String path) {
            this.path = path;
            this.payload = null;
        }

        public Message(String path, byte[] payload) {
            this.path = path;
            this.payload = payload;
        }

        public String getPath() {
            return path;
        }

        public byte[] getPayload() {
            return payload;
        }
    }
}




Java Source Code List

au.com.codeka.rps.ApplicationTest.java
au.com.codeka.rps.DebugLog.java
au.com.codeka.rps.FindingOpponentFragment.java
au.com.codeka.rps.GameActivity.java
au.com.codeka.rps.GameFragment.java
au.com.codeka.rps.MessageListenerService.java
au.com.codeka.rps.NotificationUpdateService.java
au.com.codeka.rps.PhoneConnection.java
au.com.codeka.rps.ResultFragment.java
au.com.codeka.rps.SplashActivity.java
au.com.codeka.rps.WatchConnection.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingPlayerChoiceState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.AwaitingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.DisplayingResultState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.FindingOpponentState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.GameRunningState.java
au.com.codeka.rps.game.MatchInfo.java
au.com.codeka.rps.game.ResultInfo.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.StateManager.java
au.com.codeka.rps.game.State.java
au.com.codeka.rps.game.State.java