我试图使用MessageAPI向我的手机发送一些字符串。然而,该消息在75%的时间内未能达到磨损。这是问题,它的行为至少对我来说是不可预测的。决定消息是否到达的因素是未知的。我已经阅读了之前的答案,并在发送信息之前尝试检查手表和手机是否已连接,但这也没有奏效。两者都有明显的联系,但仍然没有达到消息。以下是我的手机应用程序中的代码:
private static void sendMessage(final String path, final String duration, final String type, final String name, final String startingTime,final Context context) {
new Thread( new Runnable() {
@Override
public void run() {
Bundle inBundle = new Bundle();
inBundle.putString("duration", duration);
inBundle.putString("type", type); // will be failed
inBundle.putString("name", name); // will be failed
inBundle.putString("Starting Time", startingTime);
inBundle.putBoolean("Stop", false);
// From Bundle to bytes
byte[] inBytes = bundleToBytes(inBundle);
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mGoogleApiClient ).await();
for(Node node : nodes.getNodes()) {
MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(
mGoogleApiClient, node.getId(), path, inBytes ).await();
Log.i("Node:",nodes.getNodes().toString());
}
Wearable.NodeApi.addListener(mGoogleApiClient, new NodeApi.NodeListener() {
@Override
public void onPeerConnected(Node node) {
Log.i("Node:","Ab connected");
}
@Override
public void onPeerDisconnected(Node node) {
}
});
}
}).start();
}
以下是我的Android Wear的留言接收代码:
public void onMessageReceived(MessageEvent messageEvent) {
if( messageEvent.getPath().equalsIgnoreCase( START_ACTIVITY ) ) {
Intent intent = new Intent( this, MainActivity.class );
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent );
Bundle outBundle = jsonStringToBundle(new String(messageEvent.getData()));
String duration = outBundle.getString("duration");
stop=outBundle.getBoolean("Stop");
String type = outBundle.getString("type");
String name = outBundle.getString("name");
String startingTime= outBundle.getString("Starting Time");
Bundle b= new Bundle();
b.putInt("duration", Integer.parseInt(duration));
b.putString("Starting Time",startingTime);
Toast.makeText(getApplicationContext(),duration + " : " + startingTime,Toast.LENGTH_SHORT).show();
if(!stop) {
AcquisitionStatus=true;
Intent sensorServ = new Intent(MyService.this, SensorService.class);
sensorServ.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
sensorServ.putExtras(b);
startService(sensorServ);
}
else {
AcquisitionStatus = false;
// setAcquisitionStatus(false);
}
}
else if ( messageEvent.getPath().equalsIgnoreCase("TestPath") )
{
Toast.makeText(getApplicationContext(),"HELLO!",Toast.LENGTH_SHORT).show();
}
else {
super.onMessageReceived( messageEvent );
}
}
答案 0 :(得分:0)
尝试以下两种方法之一:
尝试设置" setResultCallback"当您发送邮件并检查是否收到回复时。
您可以使用Data API,因为Data API提供100%保证,而Message API有时会被丢弃。此外,Data API会比较先前和当前数据,并仅在至少有一个数据发生更改时才会发送。
您可以使用Data API并将数据发送为
PutDataMapRequest putDataMapRequest = PutDataMapRequest.create(*WEAR_ID*);
putDataMapRequest.getDataMap().putString("duration", duration);
putDataMapRequest.getDataMap().putString("type", type);
putDataMapRequest.getDataMap().putString("name", name);
putDataMapRequest.getDataMap().putString("Starting Time", startingTime);
putDataMapRequest.getDataMap().putBoolean("Stop", false);