Android 与可穿戴设备的通信无法正常工作

Android Wear communication with wearable not working

我有一个正在运行的智能phone 应用程序。我现在想扩展它,以便还有一个可穿戴部分。我试图从多个来源收集所有内容 (https://www.binpress.com/tutorial/a-guide-to-the-android-wear-message-api/152, and https://developer.android.com/training/building-wearables.html),但我似乎无法将数据发送到我的可穿戴设备。

我做错了什么?

这是我目前的代码...

我的基本 activity 在 phone 方面:

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();
}
}

在我的 Android Studio 内的可穿戴模块中,我有这个清单:

<?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>

我的 WearMessageListenerService 如下所示:

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);
        }
    }
}

这是我的 MainActivity.java 来自我的穿戴应用程序:

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() ));
            }
        }
    });
}

}

也在我的智能phone 应用构建配置中,我使用了这个:

wearApp project(':mywearapp')

您的主应用程序和 Wear 应用程序的包名需要完全匹配:您的包名似乎设置为 com.myapp.appname.wear(尽管您不包括您的build.gradle,可以覆盖它)。

此外,请确保在构建 GoogleApiClient 时调用 addConnectionCallbacks() - 否则它不知道在连接时给谁打电话。