MessageAPI를 사용하여 내 전화에서 내 마모에 일부 문자열을 보내려고합니다. 그러나 메시지는 약 75 %의 시간 동안 Wear에 도달하지 못합니다. 이것은 문제이며, 그 행동은 적어도 저에게는 예측할 수없는 것입니다. 메시지의 도달 여부를 결정하는 요인을 알 수 없습니다. 이전 답변을 읽었으며 메시지를 보내기 전에 시계와 전화가 연결되어 있는지 확인하려고 시도했지만이 방법도 작동하지 않았습니다. 둘 다 분명히 연결되어 있지만 여전히 메시지가 도달하지 못합니다. 다음은Android Wear로 보낸 메시지가 손실됩니다.
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);
}
}