Android可穿戴数据层不会同步。

时间:2015-09-08 06:41:40

标签: android wear-os android-wear-data-api

我正在尝试在手表和手机之间使用数据同步,但我对此失败了。

移动方面:

package com.off.testcomm;

private GoogleApiClient mGoogleApiClient;

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

    setOnClickListener((Button) findViewById(R.id.button));

    initGoogleAPIClient();

}

private void setOnClickListener(Button button) {
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Log.i("OFF", "increase counter");
            PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/count");
            putDataMapReq.getDataMap().putInt("COUNT_COUNT", 7);
            PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();

            Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
                    .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                        @Override
                        public void onResult(DataApi.DataItemResult dataItemResult) {
                            Log.i("OFF", "SaveConfig: " + dataItemResult.getStatus() + ", " + dataItemResult.getDataItem().getUri());
                        }
                    });

        }
    });
}

private void initGoogleAPIClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d("OFF", "onConnected: " + connectionHint);
                    // Now you can use the Data Layer API
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d("OFF", "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(ConnectionResult result) {
                    Log.d("OFF", "onConnectionFailed: " + result);
                }
            })
                    // Request access only to the Wearable API
            .addApi(Wearable.API)
            .build();
}

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

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

当我点击按钮时,我得到:

SaveConfig: Status{statusCode=SUCCESS, resolution=null}, wear://ad9cb4db-c697-4777-9088-0b29b8584043/count

所以我认为它已被发送。

穿着:

package com.off.testcomm;

public class MyWatchFace extends CanvasWatchFaceService {

...

private class Engine extends CanvasWatchFaceService.Engine implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,DataApi.DataListener, MessageApi.MessageListener {

    @Override
    public void onCreate(SurfaceHolder holder) {
        super.onCreate(holder);

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

    @Override
        public void onVisibilityChanged(boolean visible) {

          if (visible) {
            Log.i("OFF","is visible");
            registerReceiver();
            mGoogleApiClient.connect();
          } ...

    @Override
    public void onConnected(Bundle bundle) {
        Log.i("OFF", "OnConnected ");

        Wearable.DataApi.addListener(mGoogleApiClient, this);

    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i("OFF","onConnectionSuspended");

    }

    @Override
    public void onDataChanged(DataEventBuffer dataEventBuffer) {
        Log.i("OFF","OnDataChanged");

    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.i("OFF","onMessageReceived");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i("OFF","onConnectionFailed");

    }

 ...

我穿的最后一个日志是:&#34; OnConnected&#34; 。当我点击手机上的按钮时,似乎没有任何东西同步。

正如您所看到的,两个类都在同一个包中。

至于清单都有:

<meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

任何想法?

感谢

瓦特

1 个答案:

答案 0 :(得分:1)

由于每次点击按钮时都要添加相同的数据,因此没有&#34;更改&#34;在现有数据中,因此您不会收到onDataChanged()回调; Android Wear框架会查看您的数据内容,并确定它是否是新的内容,如果它没有看到内容的任何变化(假设它的路径也没有改变),它赢了& #39; t认为是新的。为数据添加一个时间戳,每次都应该使它成为新数据,并且应该触发回调:

putDataMapReq.getDataMap().putLong("timestamp", System.currentTimeMillis());