Android Wear sending data to Android phone, but phone appears to never receive it

百般思念 提交于 2019-11-30 20:49:39
Steven McWhorter

In order for the WearableListenerService to fire the 'onDataChanged' event, the applicationId's in the main app and wearable app must match (build.gradle files).

In addition, you cannot send static data, the data must change. That means data in your PutDataMapRequest object must be changing.

I had the same issue and from what I read on Google developers, onDataChanged will never be triggered on the device because it does not listen to events from the wearable device :

For example, you can have a handheld app that sets and gets data item objects and a wearable app that listens for these updates to update it's UI. The wearable never updates any of the data items, so the handheld app doesn't listen for any data events from the wearable app.

https://developer.android.com/training/wearables/data-layer/events.html#Listen

add a key value in the send dataapimap map.put("timestamp", System.currentmillions())

Did your onReceive gets called ??

I had a similar problem where Handheld device sends a data but Listener service do not get called. This was due to inconsistency between app and wear gradle files.

My app had multiple flavors which I had to remove and make grade same as wear and all worked smoothly there after

have u added the Wearable.DataApi.addListener() in the listener side app?

Ok, so I had this same problem, and I found out I was not connected at the time the event was sent from the mobile, so I never saw it. The key is: onDataChanged() will not fire unless the GoogleApiClient is connected and listening at the time of sending. It does not seem to send you the events you missed as soon as you reconnect; you have to manually poll for the missed DataItems when you connect().

At least, that's seems like a bug to me: I would expect onDataChanged() to fire with any events that were sent in the time the client was disconnected. Am I wrong in assuming that?

Anyway, here's how I manually polled for the events I missed each time I connect:

@Override
public void onConnected(Bundle bundle) {

    Wearable.DataApi.addListener(googleApiClient, this);

    syncPresets();
}
@Override
public void onConnectionSuspended(int i) {

    Wearable.DataApi.removeListener(googleApiClient, this);
}

private void syncPresets() {

    // Ok, so the reason is, we're calling Wearable.DataApi.addListener(googleApiClient, this);
    // only when we connect. 
    // thus, we manually poll for the ones we missed...
    Wearable.DataApi.getDataItems(googleApiClient, getDataUri(WEARABLE_DATA_PATH))
            .setResultCallback(new ResultCallback<DataItemBuffer>() {
                @Override
                public void onResult(DataItemBuffer dataItems) {
                    Status status = dataItems.getStatus();

                    if (status.isSuccess()) {

                        for (DataItem dataItem : dataItems) {

                            Log.d(TAG, "Synced image " + dataItem);
                            if (dataItem != null) {



                                // do whatever you do with the items here...
                                onReceivedImage(dataItem);



                            }
                        }
                    } else {
                        Log.d(TAG, "Failed getting image: (" + status.getStatusCode() + ") " + status.getStatusMessage());
                    }

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