Search code examples
androidwear-osandroid-wear-data-api

Sending message to disconnected mobile from wearable


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?


Solution

  • 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.