user1583010
user1583010

Reputation:

Handheld device cannot connect to GoogleApiClient

I've made a class called SendToWear with a static method called sendMessage(Context context, String path, @Nullable String data) which finds all connected nodes and sends data to any found nodes.

This class is identical to another class I used in an Android Wear device called SendToPhone which works successfully and will send a fire-and-forget message to my connected mobile phone, but SendToWear (a class with identical code, as stated) will not.

The problem is at this section:

GoogleApiClient sClient = new GoogleApiClient.Builder(context)
    .addApi(Wearable.API)
    .build();

ConnectionResult connectionResult =
        sClient.blockingConnect(5, TimeUnit.SECONDS);

if (!connectionResult.isSuccess()) {
    Log.e(TAG, "Failed to connect to sClient.");
    return 0;
} else Log.d(TAG, "sClient connected!");

The handheld device always return 'Failed to connect to sClient'. Can anybody explain this?

Thanks.

Full code below:

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.Log;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
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;
import java.util.concurrent.TimeUnit;

public class SendToPhone {

    public static int sendMessage(Context context, String path, @Nullable String data){
        Log.d(TAG, "Path: " + path + "\tData: " + data);
        ArrayList<String> nodes;
        int sent; // amount of nodes which received the message

        if(context == null || path == null){
            Log.d(TAG, "Context or Path is null.");
            return 0;
        }

        GoogleApiClient sClient = new GoogleApiClient.Builder(context)
                .addApi(Wearable.API)
                .build();

        ConnectionResult connectionResult =
                sClient.blockingConnect(5, TimeUnit.SECONDS);

        if (!connectionResult.isSuccess()) {
            Log.e(TAG, "Failed to connect to sClient.");
            return 0;
        } else Log.d(TAG, "sClient connected!");

        nodes = getNodes(sClient);
        if(nodes.size() == 0) return 0;

        for(int i = sent = 0; i < nodes.size(); i++) {
            MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                    sClient, nodes.get(i), path, data.getBytes()).await();

            // was not able to send message
            if(result.getStatus().isSuccess())
                ++sent;
        }

        sClient.disconnect();

        Log.d(TAG, "SENT: " + sent);

        return sent;

    }

    private static ArrayList<String> getNodes(GoogleApiClient client) {

        ArrayList<String> results = new ArrayList<String>();

        NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(client).await(3, TimeUnit.SECONDS);

        if(nodes == null || !nodes.getStatus().isSuccess())
            return results;

        for (Node node : nodes.getNodes()) {
            results.add(node.getId());
        }

        return results;
    }
}

Upvotes: 1

Views: 1055

Answers (3)

emir
emir

Reputation: 321

[SOLVED] It took me all day! It is really weird that, I had to change:

compile 'com.google.android.gms:play-services:10.0.1'

to

compile 'com.google.android.gms:play-services:+'

I am not sure how and why it solved the problem, though!

Upvotes: 0

Jo&#227;o M
Jo&#227;o M

Reputation: 1959

You need to connect first. Try this:

    private GoogleApiClient mGoogleApiClient;

    @Override
    public void onCreate() {
        super.onCreate();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle connectionHint) {
                        Log.d(TAG, "onConnected: " + connectionHint);
                    }

                    @Override
                    public void onConnectionSuspended(int cause) {
                        Log.d(TAG, "onConnectionSuspended: " + cause);
                        //TODO:let the user knows there was a problem. Google Service may need update or network not available
                    }
                })
                .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult connectionResult) {
                        Log.d(TAG, "onConnectionFailed: " + connectionResult);
                        //TODO:let the user knows there was a problem. Google Service may need update or network not available
                    }
                })
                .build();
        mGoogleApiClient.connect();
    }

private void doSomething(){
        if (!mGoogleApiClient.isConnected()) {
            ConnectionResult connectionResult = mGoogleApiClient.blockingConnect(TIMEOUT_MS, TimeUnit.MILLISECONDS);
            if (!connectionResult.isSuccess()) {
                Log.e(TAG, "DataLayerListenerService failed to connect to GoogleApiClient.");
                return null;
            }
        }

        //do something here
}

If still not working then ensure you have an updated version of the Google Play Service app.

Also is good to delete any previous build you may had.

In my case I had to delete the /Build folder from the Mobile and Wear projects and re-compile them....

sudo rm -rf mobile/build
sudo rm -rf wear/build
./gradlew clean
./gradlew build

Hope it helps

Upvotes: 0

user1583010
user1583010

Reputation:

Managed to fix the problem. My handheld device had the following in its build.gradle file:

compile 'com.google.android.gms:play-services:5.+'

I replaced this line with

compile 'com.google.android.gms:play-services-wearable:+'

and then it worked.

Upvotes: 1

Related Questions