1

I want to send a message from my wear to my android phone, in the wear the "SendMessageResult" is success, but it does not trigger my "showToast" method on my phone, Can someone please help me understand what I'm doing wrong?

This is my Wear Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.wear2">

<uses-feature android:name="android.hardware.type.watch" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

</manifest>

This is my Mobile manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.wear2">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name=".Lisswear">
        <intent-filter>
            <!-- listeners receive events that match the action and data filters -->
            <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
            <data android:scheme="wear" android:host="*" android:pathPrefix="/showToast" />
        </intent-filter>
    </service>

</application>

</manifest>

Wear's MainActivity.java

package com.example.user.wear2;

import android.app.Activity;
import android.content.IntentSender;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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.MessageEvent;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;

import java.io.UnsupportedEncodingException;

public class MainActivity extends Activity {

private TextView mTextView;
private Button btsent;
private boolean mbResulvingGooApiClientError = false;
private static final int GOO_API_CLIENT_REQUEST_ERROR = 1000;
private static final String Msg_Path = "/message";

private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(gooApiClientConnCallback)
            .addOnConnectionFailedListener(gooApiClientOnConnFailed)
            .build();

    final WatchViewStub stub = (WatchViewStub)                findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mTextView = (TextView) stub.findViewById(R.id.text);
            btsent =  (Button) stub.findViewById(R.id.button);
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    if(!mbResulvingGooApiClientError){
        mGoogleApiClient.connect();
    }
}

@Override
protected void onStop() {
    super.onStop();
    if(!mbResulvingGooApiClientError){
        Wearable.MessageApi.removeListener(mGoogleApiClient,wearableMsgListener);
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

public void onclickbtsent(View view){
    new AsnyTaskSendMessageToWeaarableDevice().execute();
}

private GoogleApiClient.ConnectionCallbacks gooApiClientConnCallback = new GoogleApiClient.ConnectionCallbacks(){
    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mbResulvingGooApiClientError = false;
        Wearable.MessageApi.addListener(mGoogleApiClient,wearableMsgListener);
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.d("onConnectionSuspended","0");
    }
};

private GoogleApiClient.OnConnectionFailedListener gooApiClientOnConnFailed  = new GoogleApiClient.OnConnectionFailedListener(){

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        if(mbResulvingGooApiClientError){
            return;
        }else if(connectionResult.hasResolution()){
            try{
                mbResulvingGooApiClientError = true;
                connectionResult.startResolutionForResult(MainActivity.this,GOO_API_CLIENT_REQUEST_ERROR);
            }catch (IntentSender.SendIntentException e){
                mbResulvingGooApiClientError = false;
                mGoogleApiClient.connect();
            }
        }else{
            mbResulvingGooApiClientError = false;
            Wearable.MessageApi.removeListener(mGoogleApiClient,wearableMsgListener);
        }
    }
};

private MessageApi.MessageListener wearableMsgListener = new MessageApi.MessageListener(){

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.d("onMessageReceived", String.valueOf(messageEvent.getData()));
    }
};

private class AsnyTaskSendMessageToWeaarableDevice extends AsyncTask {

    @Override
    protected Object doInBackground(Object[] objects) {
        NodeApi.GetConnectedNodesResult connectedWearableDevices = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
        for(Node node: connectedWearableDevices.getNodes()){
            String str1 = "Udemy online courses";
            try {
                byte[] arr = str1.getBytes("UTF-8");
                //String str2 = new String(arr);
                //System.out.println("new string = "+ str2);
                Log.d("node and DisplayName = ",node.getId()+ " - " + node.getDisplayName());
                MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), Msg_Path, arr).await();
                if(result.getStatus().isSuccess()){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("result.getStatus","Success");
                        }
                    });
                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d("result.getStatus","Error");
                        }
                    });
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
}


}

Mobile's MainActivity .java

package com.example.user.wear2;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
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.wearable.Wearable;

public class MainActivity extends AppCompatActivity  implements    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.d(TAG,"onConnected");
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG,"onConnectionSuspended");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG,"onConnectionFailed");
}
}

This is Lisswear.java my WearableListenerServicein in Mobile

package com.example.user.wear2;

import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;

public class Lisswear extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent messageEvent) {
    //if(messageEvent.getPath().equals("/showToast")) {
    showToast(messageEvent.getPath());
    //}
}


private void showToast(String message) {
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
lvanna9786
  • 21
  • 2
  • At a guess, you're possibly sending the message to the wrong node - i.e., not the handheld. Use the Capability API, rather than the Node API, in your Wear Activity to find the node to send to. Also, don't just rely on Toasts - you'll get much more detailed information by debugging through the various methods. – Sterling Feb 05 '17 at 14:45

1 Answers1

2

These are things you can try:

  1. make sure applicationid is the same for both devices as was the case in this SO thread
  2. make sure you have same package name
  3. make sure you have enabled permissions You can also read more on Sending and Receiving Messages from wearable to device in the docs.
Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56