/** * 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(); } } }
@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 public void onConnected(Bundle connectionHint) { if (mDataListener != null) { Wearable.DataApi.addListener(mGoogleApiClient, mDataListener); } if (mMessageListener != null) { Wearable.MessageApi.addListener(mGoogleApiClient, mMessageListener); } if (mNodeListener != null) { Wearable.NodeApi.addListener(mGoogleApiClient, mNodeListener); } // TODO Vishnu : check this callback is needed if (mConnectionCallBacks != null) { mConnectionCallBacks.onConnectionSuccess(); } }
/** * Asynchronously fetches the current config {@link DataMap} for {@link DigitalWatchFaceService} * and passes it to the given callback. * * <p>If the current config {@link DataItem} doesn't exist, it isn't created and the callback * receives an empty DataMap. */ public static void fetchConfigDataMap( final GoogleApiClient client, final FetchConfigDataMapCallback callback) { Wearable.NodeApi.getLocalNode(client) .setResultCallback( new ResultCallback<NodeApi.GetLocalNodeResult>() { @Override public void onResult(NodeApi.GetLocalNodeResult getLocalNodeResult) { String localNode = getLocalNodeResult.getNode().getId(); Uri uri = new Uri.Builder() .scheme("wear") .path(DigitalWatchFaceUtil.PATH_WITH_FEATURE) .authority(localNode) .build(); Wearable.DataApi.getDataItem(client, uri) .setResultCallback(new DataItemResultCallback(callback)); } }); }
/** * Closes the connection to Google Play services. No calls can be made using this {@link * #mGoogleApiClient} after calling this method. Any method calls that haven't executed yet will * be canceled. That is onResult(Result) won't be called, if connection to the service hasn't been * established yet all calls already made will be canceled. */ public void disConnect() { try { if (mDataListener != null) { Wearable.DataApi.removeListener(mGoogleApiClient, mDataListener); } if (mMessageListener != null) { Wearable.MessageApi.removeListener(mGoogleApiClient, mMessageListener); } if (mNodeListener != null) { Wearable.NodeApi.removeListener(mGoogleApiClient, mNodeListener); } if ((mGoogleApiClient != null) && (mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())) { mGoogleApiClient.disconnect(); } } catch (Exception e) { e.printStackTrace(); } }
@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; }