WearableListenerService's onDataChanged not called on phone

一个人想着一个人 提交于 2019-12-25 15:52:45

问题


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.

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! :)

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


}


}

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! :)


回答1:


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.

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>


来源:https://stackoverflow.com/questions/36432795/wearablelistenerservices-ondatachanged-not-called-on-phone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!