未调用OnDataChanged

时间:2016-05-21 17:23:58

标签: android-studio google-data-api android-wear-data-api

我的手机和佩戴设备之间的通信存在问题。我决定在我的应用中添加磨损模块。 Wear app只有一个类(MainActivity)

package cz.johrusk.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.widget.TextView;

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.wearable.DataApi;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.PutDataMapRequest;
import com.google.android.gms.wearable.PutDataRequest;
import com.google.android.gms.wearable.Wearable;


public class MainActivity extends Activity implements GoogleApiClient.OnConnectionFailedListener,DataApi.DataListener {

GoogleApiClient mGoogleApiClient;
private TextView mTextView;
static final String TAG = MainActivity.class.getSimpleName();

@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.text);
        }
    });
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
                @Override
                public void onConnected(Bundle connectionHint) {
                    Log.d(TAG, "onConnected: " + connectionHint);
                    sendNumber(1);
                    Log.d(TAG,"BBBBBBBB");
                }
                @Override
                public void onConnectionSuspended(int cause) {
                    Log.d(TAG, "onConnectionSuspended: " + cause);
                }
            })
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
    Log.d(TAG,"mGoogleApiClient connected;");
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG,"FAILE" + connectionResult);
}
public void sendNumber(int number) {
    PutDataMapRequest putDataMapRequest = PutDataMapRequest.create("/number");

    putDataMapRequest.getDataMap().putInt("number",number);
    putDataMapRequest.getDataMap().putLong("Time",System.currentTimeMillis());
    PutDataRequest putDataReq = putDataMapRequest.asPutDataRequest();
    putDataReq.setUrgent();
    Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq)
            .setResultCallback(new ResultCallback<DataApi.DataItemResult>() {
                @Override
                public void onResult(@NonNull DataApi.DataItemResult dataItemResult) {
                    if (!dataItemResult.getStatus().isSuccess()) {
                        Log.d(TAG,"Fail");
                    }
                    else{
                        Log.d(TAG,"Succes");
                    }
                }
            });
}
@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d(TAG,"TEST");
}

}

我认为“Wearable.DataApi.putDataItem”应该在我的手机应用程序中调用WearableListenerService。这是服务:

package cz.johrusk.showsmscode.service;


import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;


 public class WatchListener_service extends WearableListenerService {
@Override
public void onCreate() {
    super.onCreate();
   GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Wearable.API)
            .build();
    mGoogleApiClient.connect();
}
@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
    Log.d("prijato","number is: ");

    for (DataEvent dataEvent : dataEventBuffer) {
    if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
        DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
        String path = dataEvent.getDataItem().getUri().getPath();
        if (path.equals("/number")){
            int number = dataMap.getInt("numa");
            long time = dataMap.getInt("timestamp");
            Log.d("received","number is: " + number);

        }
    }
}
}

}

但是,不会调用WatchListener_service中的onDataChanged方法。 ResultCallbacks中的onResult方法打印“Succes”,因此似乎正确发送了DataItem。

我已经在Stackoverlflow上发现了很多类似的问题,所以我检查了所有这些:

  1. 两个模块都具有相同的applicationId
  2. 两个模块都使用'com.google.android.gms:play-services-wearable:9.0.0'
  3. SetUrgent用于putDataRequest,因此不应有任何延迟。
  4. WearableListenerService在Manifest中声明了正确的intent过滤器:

    动作android:name =“com.google.android.gms.wearable.DATA_CHANGED” 数据android:scheme =“wear”android:host =“*”

  5. 手机和Wear应用程序都在物理设备上运行。我的问题是......我该怎么做才能解决这个问题? 感谢

1 个答案:

答案 0 :(得分:2)

检查两个应用程序是否具有相同的调试密钥。我遇到了一些问题,问题是两个应用程序(穿戴和移动)的不同键。

PS:Android Wear API很荒谬。我不再为Wear进行开发,因为那些API的功能有多么糟糕,因为他们认为应该这样做。