2017-12-02 32 views
0

MessageAPIを使用して電話からMy Wearに文字列を送信しようとしています。しかし、そのメッセージは約75%の時間でウェアに届きません。これは問題です。その行動は、少なくとも私にとっては予測できないものです。メッセージが届くかどうかを決定する要因は不明です。私は以前の答えを読んで、メッセージを送信する前に時計と電話が接続されているかどうかをチェックしようとしましたが、これはどちらもうまくいきませんでした。どちらも明確に接続されていますが、依然としてメッセージが届きません。以下は、私の携帯電話アプリからコードされる:以下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

は2のどちらかをお試しください:

  1. 」は設定してみてくださいメッセージを送信して応答を受け取るかどうかをチェックするときに「setResultCallback」を呼び出します。

  2. データAPIはデータAPIが100%保証されているので、データAPIは使用できますが、メッセージAPIは時折削除されます。また、Data APIは前と現在のデータを比較し、少なくとも1つのデータが変更された場合にのみ送信します。あなたは、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); 
    
    としてデータを送ることができ

関連する問題