Back to project page Android.Wear.Message.
The source code is released under:
MIT License
If you think the Android project Android.Wear.Message 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.iamnbty.androidwear.helper; //from w w w . j a v a 2s. c o m import android.os.AsyncTask; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.wearable.Node; import com.google.android.gms.wearable.NodeApi; import com.google.android.gms.wearable.Wearable; import com.iamnbty.androidwear.model.ConnectedDevice; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; public class ConnectedDeviceFinder extends AsyncTask<Void, Integer, ArrayList<ConnectedDevice>> { private static final int CONNECTION_TIMEOUT_IN_MILLISECONDS = 1000; private GoogleApiClient mGoogleApiClient; private Callback mCallback; public ConnectedDeviceFinder(GoogleApiClient googleApiClient, Callback callback) { mGoogleApiClient = googleApiClient; mCallback = callback; } @Override protected void onPreExecute() { super.onPreExecute(); mCallback.onDeviceFinderStarted(this); } @Override protected ArrayList<ConnectedDevice> doInBackground(Void... params) { mGoogleApiClient.blockingConnect(CONNECTION_TIMEOUT_IN_MILLISECONDS, TimeUnit.MILLISECONDS); NodeApi.GetConnectedNodesResult result = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); List<Node> nodes = result.getNodes(); ArrayList<ConnectedDevice> connectedDevices = new ArrayList<>(); for (Node node : nodes) { connectedDevices.add(new ConnectedDevice(node.getId(), node.getDisplayName())); } return connectedDevices; } @Override protected void onPostExecute(ArrayList<ConnectedDevice> connectedDevices) { super.onPostExecute(connectedDevices); mCallback.onDeviceFinderFinished(this, connectedDevices); } public interface Callback { public void onDeviceFinderStarted(ConnectedDeviceFinder finder); public void onDeviceFinderFinished(ConnectedDeviceFinder finder, ArrayList<ConnectedDevice> connectedDevices); } }