Pew Pew
Pew Pew

Reputation: 63

Android wear data layer PutDataMapRequest doesn't work

I'm trying to implement android wear application. I guess I don't understand something, because my test project acts strangely. It's a very simple project - activity with two buttons ("Send map" and "Send message") on a mobile device and WearableListenerService on wear device, which creates notification, when data is received.

Now when I try to send data through Wearable.DataApi nothing happens on wearable - data is sent, but nothing is happening on wearable (wearable is coupled with mobile this whole time). I tried waiting 10-15 minutes, but still nothing. But if I then send data via MessageApi, message is delivered instantly, and also all my data items sent via DataApi are received at that moment.

When I try google examples that use DataApi(DataLayer or Quiz), data items are seems to be delivered almost instantly, but almost all of them use DataApi along with MessageApi.

Also, if I launch one of those examples after sending data item, all my sent items are delivered also.

So I guess my question is: is it bug in my code, that causes data items not to be delivered? Or is it supposed to work like this and data items are just accumulated on device, and actual exchange is happening, like, every hour or so, and sending message is the only way to trigger immediate delivery?

Here's my data item send code:

String data = String.valueOf(System.currentTimeMillis());
PutDataMapRequest pdmr = PutDataMapRequest.create("/wear_test/"+data);
pdmr.getDataMap().putString("1", data);

PutDataRequest pdr = pdmr.asPutDataRequest();

Wearable.DataApi.putDataItem(googleApiClient,pdr);

Here's listener service code:

public class ListenerService extends WearableListenerService {
    private static final String TAG = ListenerService.class.getSimpleName();

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "Service created");
    }



    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        super.onDataChanged(dataEvents);
        Log.e(TAG, "onDataChanged(DataEventBuffer dataEvents) is called");


        for (DataEvent dataEvent : dataEvents) {
            Log.e(TAG, "Event type: "+dataEvent.getType()+", Uri:'"+dataEvent.getDataItem().getUri());
            if(dataEvent.getType()==DataEvent.TYPE_CHANGED && dataEvent.getDataItem().getUri().getPath().contains("/wear_test")){
                DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
                String data = dataMap.getString("1");
                Log.e(TAG, "Data received: " + data);

                createNotification(data,505, "data map");
            }
        }
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);
        if (messageEvent.getPath().contains("wear_test")){
            String messageStr = new String(messageEvent.getData(), Charset.forName("UTF-8"));
            Log.e(TAG, "Message received:"+messageStr);
            createNotification(messageStr,808, "message");
        }
    }

    private void createNotification(String data, int notificationCode, String suffix){
        Notification notification = new Notification.Builder(this)
                .setTicker(data)
                .setSmallIcon(R.drawable.common_google_signin_btn_icon_light)
                .setContentText(data+"\n"+suffix)
                .setSubText(data+"\n"+suffix)
                .build();

        ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                .notify(notificationCode, notification);
    }
}

Upvotes: 2

Views: 664

Answers (1)

9and3r
9and3r

Reputation: 245

I had the same problem. I solved it using PutDataRequest.setUrgent().

From the documentation:

DataItems will be delayed no longer than 30 minutes, subject to a connected peer, but are expected to arrive much sooner. Clients should only setUrgent() for DataItems which need to be delivered right away.

Upvotes: 1

Related Questions