0

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')
user754730
  • 1,341
  • 5
  • 31
  • 62

1 Answers1

3

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks alot! I did that but still it seems like onConnected() is not called on the mobile side... – user754730 May 15 '15 at 21:16
  • 1
    Where do you call `initGoogleApiClient()`? You haven't included your `onCreate()` or a similar method – ianhanniballake May 15 '15 at 21:17
  • No sorry I forgot to include it in my post, will update right now. I'm calling it from onCreate()... This is called and also the APIClient is instantiated but the onConnected() is not called... – user754730 May 15 '15 at 21:19
  • 1
    Ah, looks like you are missing a call to addConnectionCallbacks() on your `GoogleApiClient.Builder`! That'll do it. – ianhanniballake May 15 '15 at 21:21