1

How do I implement a request-response protocol for an Android Wear 2.0 app?

Scenario:

When I tap on a button on the watch, I want it to fetch some data from the phone and display it on the watch's screen.

What I tried:

I implemented a working example using the MessageApi, but I don't like it. I send a dummy "request" in one place using one method, I disregard the PendingResult and then hope that eventually I will receive a message that will be a corresponding response.

Ideally, what I'd like to have is:

byte[] responseBytes = sendRequest(someRequestBytes);

DogeLion
  • 3,371
  • 5
  • 16
  • 24
  • Your "ideal" solution is a synchronous function call, which isn't how inter-device communication works. Wireless data transmission takes time, which is why the Wear data and message APIs are structured asynchronously. – Sterling Aug 15 '17 at 04:17
  • @String I'm totally happy with an asynchronous request-response API - Futures, Promises, all that. That's what the title and body of the question is about. – DogeLion Aug 18 '17 at 10:07
  • Hmm. In your question you say you've **already** implemented this asynchronously, but you're not happy with it - that you'd prefer a synchronous solution. I didn't see any acknowledgement that synchronous wasn't supported by the architecture. – Sterling Aug 18 '17 at 14:15
  • @String I'm not happy because it's not a request-response API. I send a request in one place using one method and then *hope* to receive a message with a response. And I want something like in my snippet above (+- a `Future` wrapper) – DogeLion Aug 18 '17 at 14:32
  • And to further clarify: I have something like Vyacheslav wrote below. I just don't like it and I'll probably wrap the API.. unless Android provides a request-response API. – DogeLion Aug 18 '17 at 14:38
  • Then there's your answer. You want a different API than what Google has provided for Wear. Unless someone else has built a "wrapper" for the Wear APIs in the form that you want (I'm not aware of one, but it's possible that one exists), you'll need to build such a wrapper yourself from the APIs that **are** provided. – Sterling Aug 18 '17 at 15:55

1 Answers1

0

I'm not sure what you have tried.

But this code should work to send a byte array.

Wearable.MessageApi.sendMessage(googleApiClient, transcriptionNodeId,
            VOICE_TRANSCRIPTION_MESSAGE_PATH, voiceData).setResultCallback(
                  new ResultCallback() {
                      @Override
                      public void onResult(SendMessageResult sendMessageResult) {
                          if (!sendMessageResult.getStatus().isSuccess()) {
                              // Failed to send message
                          }
                      }
                  }
            );

voiceData is a simple byte array. This array will be received by both wearable and handheld device.

https://developer.android.com/training/wearables/data-layer/messages.html

To retrieve the data use this:

@Override
public void onMessageReceived(MessageEvent messageEvent) {
    if (messageEvent.getPath().equals(YOUR_TEXT)) {
        messageEvent.getData();//this is your byte array
    }
}
TofferJ
  • 4,678
  • 1
  • 37
  • 49
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194