WearableListenerService的onDataChanged未在电话上调用 [英] WearableListenerService's onDataChanged not called on phone

查看:96
本文介绍了WearableListenerService的onDataChanged未在电话上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在可穿戴设备上有一个应用程序,应在单击按钮后将数据映射发送到手持设备。我已经从手持设备到手机进行了几乎相同的设置,唯一的不同是我发送了一条消息,效果很好,现在我要发送 DataMap 另一种方式。

I have an app on the wearable that should send a datamap to the handheld on a button click. I've made almost the same setup from the handheld to the phone, only difference is that I sent a message, which works perfectly, and now I want to send a DataMap the other way.

我很确定我的设置正确,但 onDataChanged()永远不会拨打电话上的> wearableListenerService 。我忘记了重要的事情还是还有什么不对的地方?

I am pretty sure I have the correct setup but still the onDataChanged() on the wearableListenerService on the phone is never called. Have I forgot something important or what else is wrong?

请看看并保存我的一天! :)

Please take a look and save my day! :)

这里是从可穿戴设备发送数据映射的线程(从可穿戴设备的mainActivity调用,googleClient存在并且我已经启动了该线程)。调试返回该数据映射已成功发送。

Here is the thread that sends the datamap from the wearable (called from the wearable's mainActivity, the googleClient exists and I've started the thread). Debugging return that the datamap was successfully sent.

public class SendDataMapToHandheldDataLayer_Thread extends Thread {

private String path;
private DataMap dataMap;
private GoogleApiClient googleClient;

public SendDataMapToHandheldDataLayer_Thread(String cPath, DataMap cDataMap, GoogleApiClient cGoogleClient){
    path = cPath;
    dataMap = cDataMap;
    googleClient = cGoogleClient;
}


public void run(){
    PutDataMapRequest putDMR = PutDataMapRequest.create(path);
    putDMR.getDataMap().putAll(dataMap);
    PutDataRequest request = putDMR.asPutDataRequest();
    DataApi.DataItemResult result = Wearable.DataApi.putDataItem(googleClient, request).await();
    if(result.getStatus().isSuccess()){
        Log.v("dataMapSender_Wear", "DataMap successfully sent!");
    }else{
        Log.v("dataMapSender_Wear", "ERROR: Failed to send DataMap to data layer");
    }


}


}

这是电话上的监听器,从未被调用。为什么?它基本上是从android开发人员教程中复制的,该教程位于: http:/ /developer.android.com/training/wearables/data-layer/events.html#Listen 。我也尝试过自己的版本,但我认为问题出在其他地方。

And here is the listener on the phone, which is never called. Why? It's essentially just copied from the android developer tutorial found here: http://developer.android.com/training/wearables/data-layer/events.html#Listen . I have also tried own versions but I think the problem lies somewhere else.

public class ListenerServiceMobile extends WearableListenerService{



private static final String TAG = "DataLayerSample";
private static final String START_ACTIVITY_PATH = "/start-activity";
private static final String DATA_ITEM_RECEIVED_PATH = "/data-item-received";

@Override
public void onDataChanged(DataEventBuffer dataEvents) {

    Toast.makeText(getApplicationContext(), "Data changed!", Toast.LENGTH_LONG).show();


    if (Log.isLoggable(TAG, Log.DEBUG)) {
        Log.d(TAG, "onDataChanged: " + dataEvents);
    }
    final List<DataEvent> events = FreezableUtils
            .freezeIterable(dataEvents);

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

    ConnectionResult connectionResult =
            googleApiClient.blockingConnect(30, TimeUnit.SECONDS);

    if (!connectionResult.isSuccess()) {
        Log.e(TAG, "Failed to connect to GoogleApiClient.");
        return;
    }

    // Loop through the events and send a message
    // to the node that created the data item.
    for (DataEvent event : events) {
        Uri uri = event.getDataItem().getUri();

        // Get the node id from the host value of the URI
        String nodeId = uri.getHost();
        // Set the data of the message to be the bytes of the URI
        byte[] payload = uri.toString().getBytes();

        // Send the RPC
        Wearable.MessageApi.sendMessage(googleApiClient, nodeId,
                DATA_ITEM_RECEIVED_PATH, payload);
    }
}

手机的清单文件包含以下内容:

The mobile's manifest file contains this:

<service android:name=".ListenerServiceMobile">
        <intent-filter>
            <action android:name="com.google.android.gms.wearable.DATA_CHANGED"></action>
            <data android:scheme="wear" android:host="*" android:pathPrefix="/prefix" />
        </intent-filter>
    </service>

谢谢您的回答! :)

Thank you for answering! :)

推荐答案

我将从删除 android:pathPrefix 开始它正在过滤您的服务正在侦听的数据。您应该看到 onDataChange()现在被调用。

I would start by removing the android:pathPrefix since it's filtering the data your service is listening for. You should see the onDataChange() being called now.

看到数据之后,您可以转到返回并将 pathPrefix 设置为可穿戴设备中要设置的路径。 (我猜/ data-item-received)。路径前缀与在主机之后指定的任何内容的开头匹配。

After you see the data come across, you can go back and set the pathPrefix to whatever you are setting the path to in your Wearable. (I am guessing /data-item-received). The pathPrefix matches the beginning of whatever you specify after the host.

在这种情况下,您要使用的是以下内容:

Here is what you want if that is the case:

<service android:name=".ListenerServiceMobile">
  <intent-filter>
      <action android:name="com.google.android.gms.wearable.DATA_CHANGED" />
      <data android:scheme="wear" android:host="*"
               android:pathPrefix="/data-item-received"" />
  </intent-filter>
</service>

这篇关于WearableListenerService的onDataChanged未在电话上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆