Search code examples
javaandroidwear-osandroid-wear-data-api

Android Wear communication with wearable not working


I have a smartphone app which is working. I now want to extend it so that there is also a wearable part. I tried to get everything together from multiple sources (https://www.binpress.com/tutorial/a-guide-to-the-android-wear-message-api/152, and https://developer.android.com/training/building-wearables.html) but I can't seem to send my data to my wearable.

What am I doing wrong?

This is the code I have so far...

My basic activity on the phone side:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks

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

    // Android wear support
    initGoogleApiClient();
}

// Android wear support
private void initGoogleApiClient() {
    mApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();
    mApiClient.isConnected());

    mApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    sendMessage(START_ACTIVITY, "Test 12345");
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mApiClient.isConnected()) {
        mApiClient.disconnect();
    }
}

private void sendMessage(final String path, final String text) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mApiClient).await();
            for (Node node : nodes.getNodes()) {
                MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
                        mApiClient, node.getId(), path, text.getBytes()).await();
            }
        }
    }).start();
}
}

In my wearable module inside Android Studio I have this manifest:

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

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    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>

    <service android:name="com.myapp.appname.wear.WearMessageListenerService">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>
</application>

My WearMessageListenerService looks like this:

public class WearMessageListenerService extends WearableListenerService {
    private static final String START_ACTIVITY = "/start_activity";

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        if (messageEvent.getPath().equalsIgnoreCase(START_ACTIVITY)) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        } else {
            super.onMessageReceived(messageEvent);
        }
    }
}

And this is my MainActivity.java from my wear app:

public class MainActivity extends Activity implements MessageApi.MessageListener, GoogleApiClient.ConnectionCallbacks {

private TextView mTextView;
private static final String WEAR_MESSAGE_PATH = "/start_activity";
GoogleApiClient mApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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.textView);
        }
    });

    mApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(this)
            .build();

    mApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    Wearable.MessageApi.addListener(mApiClient, this);
}

@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onMessageReceived( final MessageEvent messageEvent ) {
    runOnUiThread( new Runnable() {
        @Override
        public void run() {
            if( messageEvent.getPath().equalsIgnoreCase( WEAR_MESSAGE_PATH ) ) {
                mTextView.setText( new String( messageEvent.getData() ));
            }
        }
    });
}

}

Also in my smartphone app build config I use this:

wearApp project(':mywearapp')

Solution

  • The package name of your main app and Wear app need to match exactly: it appears your package name is set to com.myapp.appname.wear (although you do not include your build.gradle, which could override that).

    Also, make sure you call addConnectionCallbacks() when building your GoogleApiClient - without that it doesn't know who to call when connected.