2017-12-02 22 views
0

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); 
    } 
} 

답변

0

는이 중 하나를 시도해보십시오 아래는 내 전화 응용 프로그램에서 코드 설정해보십시오

  1. 는 " setResultCallback "메시지를 보내고 응답을 받았는지 확인하십시오.

  2. Data API는 데이터 API가 100 % 보장되는 반면 Message API는 때때로 삭제됩니다. 또한 데이터 API는 이전 데이터와 현재 데이터를 비교하여 데이터 중 하나 이상이 변경된 경우에만 데이터를 보냅니다.

당신은 데이터 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); 
로 데이터를 보낼 수