Android Open Source - Android.Wear.Message Connected Device Finder






From Project

Back to project page Android.Wear.Message.

License

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.

Java Source Code

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);

    }

}




Java Source Code List

com.iamnbty.androidwear.ApplicationTest.java
com.iamnbty.androidwear.MainActivity.java
com.iamnbty.androidwear.MainActivity.java
com.iamnbty.androidwear.MessageListenerService.java
com.iamnbty.androidwear.helper.ConnectedDeviceFinder.java
com.iamnbty.androidwear.helper.GoogleApiHelper.java
com.iamnbty.androidwear.model.ConnectedDevice.java
com.iamnbty.androidwear.service.ListenerService.java