Evolutio
Evolutio

Reputation: 974

can't send message from wear to my phone app

I want to send data from my Wear to the PhoneApp. I created a phone app with this AndroidManifest.xml

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

    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />

    <application
        android:allowBackup="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service android:name=".services.ListenerService" >
            <intent-filter>
                <action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
                <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
                <!-- <data android:scheme="wear" android:host="*" android:pathPrefix="/updatecar" /> -->
            </intent-filter>
        </service>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

my ListenerService:

package sh.evolutio.car.services;

import android.content.Intent;
import android.util.Log;

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

public class ListenerService extends WearableListenerService {
  private static final String TAG = "ListenerService";
  private static final String MESSAGE_PATH = "/updatecar";

  @Override
  public void onCreate() {
      Log.d(TAG, "ListenerService created");
  }

  @Override
  public void onMessageReceived(MessageEvent messageEvent) {
      Log.d(TAG, "onMessageReceived");

      if (messageEvent.getPath().equals(MESSAGE_PATH)) {
          Log.d(TAG, "good message");
      } else {
          Log.d(TAG, "bad message");
      }
  }
}

my MainActivity with this onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);

  startService(new Intent(MainActivity.this, ListenerService.class));
}

When I start the App on my phone I got in the Logcat:

26131-26131/sh.evolutio.car D/ListenerService: ListenerService created

When I send with the wearapp some data to my phone, my ListenerService didn't fire the onMessageReceived method..

Here is my AndroidManifest from the wearapp:

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

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

  <uses-permission android:name="android.permission.WAKE_LOCK" />

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@android:style/Theme.DeviceDefault">
    <uses-library
      android:name="com.google.android.wearable"
      android:required="true" />
    <meta-data
      android:name="com.google.android.wearable.standalone"
      android:value="false" />

    <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>
</manifest>

The MainActivity from the wearapp looks like this:

package sh.evolutio.carwear;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.wearable.activity.WearableActivity;
import android.util.Log;
import android.view.View;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
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;

public class MainActivity extends WearableActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  private static String TAG = "MainActivity";
  private static final String MESSAGE_PATH = "/updatecar";

  Node mNode; // the connected device to send the message to
  GoogleApiClient mGoogleApiClient;
  private boolean mResolvingError = false;

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

    //Connect the GoogleApiClient
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    sendMessage("test");

    // Enables Always-on
    setAmbientEnabled();
  }

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

  /**
    * Resolve the node = the connected device to send the message to
    */
  private void resolveNode() {
    Log.d(TAG, "resolveNode");

    Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
      .setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() {
        @Override
        public void onResult(NodeApi.GetConnectedNodesResult nodes) {
          for (Node node : nodes.getNodes()) {
            Log.d(TAG, "resolvedNode: " + node);
            mNode = node;
          }
        }
      });
  }

  @Override
  public void onConnected(Bundle bundle) {
    resolveNode();
  }

  @Override
  public void onConnectionSuspended(int i) {}

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

  /**
    * Send message to mobile handheld
    */
  private void sendMessage(String Key) {
    if (mNode != null && mGoogleApiClient!= null && mGoogleApiClient.isConnected()) {
      final String messageKey = Key;

      Log.d(TAG, "isConnected: " + mGoogleApiClient.isConnected());
      Log.d(TAG, "connected to: " + mNode.getId());

      Task<Integer> sendTask = Wearable.getMessageClient(MainActivity.this).sendMessage(mNode.getId(), MESSAGE_PATH, messageKey.getBytes());
      sendTask.addOnSuccessListener(new OnSuccessListener<Integer>() {
        @Override
        public void onSuccess(Integer integer) {
          Log.d(TAG, "onSuccess: " + integer);
        }
      });

      sendTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
          Log.d(TAG, "onFailure: " + e.getMessage());
        }
      });

      Wearable.MessageApi.sendMessage(mGoogleApiClient, mNode.getId(), MESSAGE_PATH, messageKey.getBytes()).setResultCallback(
        new ResultCallback<MessageApi.SendMessageResult>() {
          @Override
          public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) {
            if (sendMessageResult.getStatus().isSuccess()) {
              Log.v(TAG, "Message: { " + messageKey + " } sent to: " + mNode.getDisplayName());
            } else {
              // Log an error
              Log.v(TAG, "ERROR: failed to send Message");
            }
          }
        }
      );
    }
  }
}

When I the message got send, i got this in the logcat from the wearapp:

sh.evolutio.carwear D/MainActivity: isConnected: true
sh.evolutio.carwear D/MainActivity: connected to: 778d0d53
sh.evolutio.carwear V/MainActivity: Message: { forward } sent to: HUAWEI Mate 10 Pro
sh.evolutio.carwear D/MainActivity: onSuccess: 17282

so the message was sent to my Mate 10 Pro. But why my Mate 10 Pro App can't receive the Message? Where is my mistake? I didn't find it.

Upvotes: 0

Views: 217

Answers (1)

Thomas Thomas
Thomas Thomas

Reputation: 884

In your mobile activity, do not start the service manually. The service will be started automatically by Android on reception of the message. And you need to uncomment your data pathPrefix definition in the AndroidManifest.

Upvotes: 0

Related Questions