[英]Request - Response API for Android Wear 2.0?
How do I implement a request-response protocol for an Android Wear 2.0 app? 如何为Android Wear 2.0应用实现请求-响应协议?
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. 我使用
MessageApi
实现了一个工作示例,但我不喜欢它。 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. 我使用一种方法在一个地方发送一个虚拟的“请求”,而忽略了
PendingResult
,然后希望最终我将收到一条消息,该消息将是相应的响应。
Ideally, what I'd like to have is: 理想情况下,我想拥有的是:
byte[] responseBytes = sendRequest(someRequestBytes);
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. voiceData是一个简单的字节数组。 This array will be received by both wearable and handheld device.
该阵列将由可穿戴设备和手持设备接收。
https://developer.android.com/training/wearables/data-layer/messages.html 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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.