Back to project page android_wear.
The source code is released under:
MIT License
If you think the Android project android_wear listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.smashedlabs.torchwear; //w ww. j a v a 2s. c o m import android.app.Activity; import android.os.Bundle; import android.support.wearable.view.WatchViewStub; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; 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.common.api.Status; import com.google.android.gms.wearable.MessageApi; import com.google.android.gms.wearable.MessageEvent; import com.google.android.gms.wearable.Node; import com.google.android.gms.wearable.NodeApi; import com.google.android.gms.wearable.Wearable; import com.google.android.gms.wearable.WearableStatusCodes; public class MainWearActivity extends Activity { private TextView mTextView; private Button mToggleButton; private Button mPingButton; Node node; // the connected device to send the message to GoogleApiClient mGoogleApiClient; public final String TAG = MainWearActivity.this.getClass().getSimpleName().toString(); public String mMessage = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_wear); mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .build(); mGoogleApiClient.connect(); final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { mToggleButton = (Button) stub.findViewById(R.id.toggle_button); if(mToggleButton != null) { mToggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mMessage = "LED"; fireMessage(); } }); } mPingButton = (Button) stub.findViewById(R.id.ping_button); if(mPingButton != null) { mPingButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mMessage = "PING"; fireMessage(); } }); } } }); } private void fireMessage() { // Send the RPC PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient); nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult result) { for (int i = 0; i < result.getNodes().size(); i++) { Node node = result.getNodes().get(i); String nName = node.getDisplayName(); String nId = node.getId(); Log.d(TAG, "Node name and ID: " + nName + " | " + nId); Wearable.MessageApi.addListener(mGoogleApiClient, new MessageApi.MessageListener() { @Override public void onMessageReceived(MessageEvent messageEvent) { Log.d(TAG, "Message received: " + messageEvent); } }); PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), mMessage, null); messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { Status status = sendMessageResult.getStatus(); Log.d(TAG, "Status: " + status.toString()); if (status.getStatusCode() != WearableStatusCodes.SUCCESS) { Toast.makeText(MainWearActivity.this, "Error - Try again", Toast.LENGTH_SHORT).show(); } } }); } } }); } }