Using GoogleApiClient from Push Notification IntentService to Send Data to Android Wear

回眸只為那壹抹淺笑 提交于 2019-12-04 08:36:31

It looks like your data isn't ever changing. When you're using the DataItem API approach, don't think of it as sending data over from the device to the wearable (or vice versa). Instead, remember that you're just manipulating a cache that is shared across devices: you're syncing data, not transferring it.

This ultimately means that your listener won't trigger unless the data is changed. That's why the method is called onDataChanged(). The underlying API handles updating the cache with your PutDataRequest intelligently to be much more efficient. If you're syncing a payload that contains the same information, you won't notice anything.

So try this: add a timestamp to your payload. Maybe something like putDataMapRequest.getDataMap().putLong("timestamp", System.currentTimeMillis()); Your payload will be different each time, and you should see that onDataChanged() will now trigger.

Yeah, it took me a while to figure this out!

First of all, if this is your full code, you're not actually calling connect() on the GoogleApiClient.

Secondly, connect() is asynchronous, so you cannot just do something like

mGoogleApiClient.connect();
if (mGoogleApiClient.isConnected()) ... 

You need to wait for the connection callbacks instead. Or, if you're executing this code in a background thread, you can use blockingConnect() instead.

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