1

I'm trying to send an event or better a message to the mobile while the wearable is disconnected.

Here is the code I'm using:

Wearable.MessageApi.sendMessage(
        mGoogleApiClient, node, event, message).setResultCallback(
        new ResultCallback<MessageApi.SendMessageResult>() {
            @Override
            public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                if(!sendMessageResult.getStatus().isSuccess()) {
                    Log.e(TAG, "Failed to send message with status code: "
                            + sendMessageResult.getStatus().getStatusCode());
                }
            }
        }
);

The node ID is cached when onPeerConnected(Node peer) is called so I don't need to query the Node API to get an empty list. However I send the data to the node which is offline. That results the StatusCode 4000 which is TARGET_NODE_NOT_CONNECTED. Of course I know that, but what is the best way to cache this event to send it as soon as possible?

rekire
  • 47,260
  • 30
  • 167
  • 264
  • Why don't you just use the DataApi? Google Play services will do the "caching" for you. Otherwise you need to implement it on your own and the best way depends on your needs - Do you need to save this event in persistent storage or just as a variable? Later, all you need to do is to resend this event once the node is connected again. Although consider using DataApi, it seems like a more appropriate choice for you. – Maciej Ciemięga Feb 18 '15 at 09:58
  • I would write a file, In that way I wont be loosing any data even after device goes reboot. – TheLittleNaruto Feb 18 '15 at 10:13

1 Answers1

0

I ended in the that idea which Maciej Ciemięga pointed out in the comments. I'm using the DataAPI to store and forward my events. After the event was recieved by the mobile I delete the path from the data layer, since it did its job.

You should keep in mind the deletion will invoke again the onDataChanged method. So you should check the type of the DataEvent:

event.getType() == DataEvent.TYPE_DELETED

If you don't keep that in mind you may get an infinity loop.

rekire
  • 47,260
  • 30
  • 167
  • 264