/**
   * Sends byte[] data to the specified node.
   *
   * @param messagePath The path of the message
   * @param message The message to send
   * @param callback The callback to receive the response
   * @param isAsynchronous send data asynchronously
   */
  public void sendMessage(
      final String messagePath,
      final String message,
      final ResultCallback<MessageApi.SendMessageResult> callback,
      final boolean isAsynchronous) {
    if (!isConnected()) {
      if (mConnectionCallBacks != null) {
        mConnectionCallBacks.onConnectionFailed(WearConnectionCallBacks.NETWORK_ERROR);
      }
      return;
    } else if (messagePath == null) {
      if (mConnectionCallBacks != null) {
        mConnectionCallBacks.onConnectionFailed(WearConnectionCallBacks.PATH_NULL_ERROR);
      }
      return;
    }

    if (isAsynchronous) {
      Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
          .setResultCallback(
              new ResultCallback<NodeApi.GetConnectedNodesResult>() {
                @Override
                public void onResult(NodeApi.GetConnectedNodesResult connectedNodesResult) {
                  List<Node> connectedNodes = connectedNodesResult.getNodes();

                  for (Node node : connectedNodes) {

                    String nodeId = node.getId();
                    PendingResult<MessageApi.SendMessageResult> messageResult =
                        Wearable.MessageApi.sendMessage(
                            mGoogleApiClient, nodeId, messagePath, message.getBytes());
                    if (callback != null) {
                      messageResult.setResultCallback(callback);
                    }
                  }
                }
              });
    } else {
      if (isRunningOnMainThread()) {
        if (mConnectionCallBacks != null) {
          mConnectionCallBacks.onConnectionFailed(
              WearConnectionCallBacks.METHOD_CALLED_FROM_UI_THREAD);
        }
        return;
      }
      NodeApi.GetConnectedNodesResult connectedNodesResult =
          Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
      List<Node> connectedNodes = connectedNodesResult.getNodes();

      for (Node node : connectedNodes) {

        String nodeId = node.getId();
        Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId, messagePath, message.getBytes())
            .await();
      }
    }
  }
Beispiel #2
0
    @Override
    protected Void doInBackground(Void... args) {
      NodeApi.GetConnectedNodesResult nodes =
          Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();

      if (nodes.getNodes().size() > 0) {
        Wearable.MessageApi.sendMessage(
                mGoogleApiClient, nodes.getNodes().get(0).getId(), GET_HELP, "".getBytes())
            .setResultCallback(
                new ResultCallback<MessageApi.SendMessageResult>() {
                  @Override
                  public void onResult(MessageApi.SendMessageResult sendMessageResult) {
                    if (!sendMessageResult.getStatus().isSuccess()) {
                      Log.d(
                          "WATCH OUTPUT",
                          "Failed to send message with status code: "
                              + sendMessageResult.getStatus().getStatusCode());
                    } else {
                      Log.d("WATCH OUTPUT", "Successfully requested train times");
                    }
                  }
                });
      }

      return null; // return value doesn't matter...
    }
 private Collection<String> getNodes() {
   HashSet<String> results = new HashSet<String>();
   NodeApi.GetConnectedNodesResult nodes =
       Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await();
   for (Node node : nodes.getNodes()) {
     results.add(node.getId());
   }
   return results;
 }
 private void descobrirNoDestino() {
   Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
       .setResultCallback(
           new ResultCallback<NodeApi.GetConnectedNodesResult>() {
             @Override
             public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
               mNodes = getConnectedNodesResult.getNodes();
             }
           });
 }
 private void resolveNode() {
   Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
       .setResultCallback(
           new ResultCallback<NodeApi.GetConnectedNodesResult>() {
             @Override
             public void onResult(NodeApi.GetConnectedNodesResult nodes) {
               for (Node node : nodes.getNodes()) {
                 mNode = node;
               }
             }
           });
 }
 private void descobrirNoDestino() {
   Wearable.NodeApi.getConnectedNodes(mGoogleApiClient)
       .setResultCallback(
           new ResultCallback<NodeApi.GetConnectedNodesResult>() {
             @Override
             public void onResult(NodeApi.GetConnectedNodesResult getConnectedNodesResult) {
               List<Node> nodes = getConnectedNodesResult.getNodes();
               if (nodes != null && nodes.size() > 0) {
                 Node node = nodes.get(0);
                 mNodeId = node.getId();
               }
             }
           });
 }
    public void run() {

      NodeApi.GetConnectedNodesResult nodes =
          Wearable.NodeApi.getConnectedNodes(googleClient).await();
      for (Node node : nodes.getNodes()) {
        MessageApi.SendMessageResult result =
            Wearable.MessageApi.sendMessage(googleClient, node.getId(), path, message.getBytes())
                .await();
        if (result.getStatus().isSuccess()) {
          Log.v("myTag", "Message: {" + message + "} sent to: " + node.getDisplayName());
        } else {
          // Log an error
          Log.v("myTag", "ERROR: failed to send Message");
        }
      }
    }
    @Override
    protected Void doInBackground(Void... params) {
      Dexcom dexcom = new Dexcom();

      try {
        String token =
            dexcom.login(
                PreferenceManager.getDefaultSharedPreferences(context).getString("username", ""),
                PreferenceManager.getDefaultSharedPreferences(context).getString("password", ""));
        if (token != null && token.length() > 0 && token.startsWith("\"")) {
          token = token.substring(1, token.length() - 2); // Strip the "s
          SharedPreferences.Editor editor =
              PreferenceManager.getDefaultSharedPreferences(context).edit();
          editor.putString("token", token);
          editor.apply();
          JSONArray a = dexcom.latestGlucoseValues(token, 1440, 1);
          if (a != null && a.length() > 0) {
            JSONObject o = a.getJSONObject(0);
            final int trend = o.getInt("Trend");
            final int value = o.getInt("Value");
            String WT = o.getString("WT");
            final long time = Long.valueOf(WT.substring(6, WT.length() - 2));
            Log.i("DexcomShareDashclock", "Latest glucose reading: " + value + " mg/dL");

            editor.putInt("Trend", trend);
            editor.putInt("Value", value);
            editor.putLong("Time", time);
            editor.apply();

            final GoogleApiClient googleApiClient =
                new GoogleApiClient.Builder(context).addApi(Wearable.API).build();

            ConnectionResult connectionResult =
                googleApiClient.blockingConnect(30, TimeUnit.SECONDS);

            if (!connectionResult.isSuccess()) {
              Log.e("DexcomShareDashclock", "Failed to connect to GoogleApiClient.");
              return null;
            }

            DataMap map = new DataMap();
            map.putInt("trend", trend);
            map.putInt("value", value);
            map.putLong("time", time);

            NodeApi.GetConnectedNodesResult nodes =
                Wearable.NodeApi.getConnectedNodes(googleApiClient).await();
            for (Node node : nodes.getNodes()) {
              Wearable.MessageApi.sendMessage(
                      googleApiClient, node.getId(), "/latest_glucose", map.toByteArray())
                  .await();
            }

            googleApiClient.disconnect();
          }
        } else {
          Log.e("Dexcom", "Response: " + token);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
      return null;
    }