/** * Handle the message sending action in the provided background thread. */ private void handleActionSendMessage(String mode) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .build(); if (!(mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())) { mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT, TimeUnit.SECONDS); } NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); // Send the Do Not Disturb mode message to all devices (nodes) for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), DND_SYNC_PREFIX, mode.getBytes()).await(); if (!result.getStatus().isSuccess()){ Log.e(TAG, "Failed to send message to " + node.getDisplayName()); } else { Log.i(TAG, "Successfully sent message " + mode + " to " + node.getDisplayName()); } } }
/** * Handle the message sending action in the provided background thread. */ private void handleActionSendMessage(String mode) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .build(); if (!(mGoogleApiClient.isConnected() || mGoogleApiClient.isConnecting())) { mGoogleApiClient.blockingConnect(CONNECTION_TIME_OUT, TimeUnit.SECONDS); } NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); // Send the Do Not Disturb mode message to all devices (nodes) for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), DND_SYNC_PREFIX, mode.getBytes()).await(); if (!result.getStatus().isSuccess()){ Log.e(TAG, "Failed to send message to " + node.getDisplayName()); } else { Log.i(TAG, "Successfully sent message " + mode + " to " + node.getDisplayName()); } } //mGoogleApiClient.disconnect(); }
private void sendMessage(String message) { if (mNode != null && mGoogleApiClient != null) { Wearable.MessageApi.sendMessage(mGoogleApiClient, mNode.getId(), WEAR_PATH, message.getBytes()) .setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { if (!sendMessageResult.getStatus().isSuccess()) { Log.d("packtchat", "Failed message: " + sendMessageResult.getStatus().getStatusCode()); } else { Log.d("packtchat", "Message succeeded"); } } }); } }
private void sendMessage(String city) { if (mNode != null && mGoogleApiClient != null) { Wearable.MessageApi.sendMessage(mGoogleApiClient, mNode.getId(), WEAR_PATH, city.getBytes()) .setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { if (!sendMessageResult.getStatus().isSuccess()) { Log.d("packtchat", "Failed message: " + sendMessageResult.getStatus().getStatusCode()); } else { Log.d("packtchat", "Message succeeded"); } } }); } }
/** * Sends the given message to the handheld. * @param path message path * @param message the message */ private void sendMessageToHandheld(final @NonNull Context context, final @NonNull String path, final @NonNull String message) { new Thread(new Runnable() { @Override public void run() { final GoogleApiClient client = new GoogleApiClient.Builder(context) .addApi(Wearable.API) .build(); client.blockingConnect(); final NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(client).await(); for(Node node : nodes.getNodes()) { final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(client, node.getId(), path, message.getBytes()).await(); if (!result.getStatus().isSuccess()){ Log.w(TAG, "Failed to send " + path + " to " + node.getDisplayName()); } } client.disconnect(); } }).start(); }
/** * Sends the given command to the handheld. * * @param command the message */ private void sendMessageToHandheld(final @NonNull Context context, final @NonNull String command) { new Thread(new Runnable() { @Override public void run() { final GoogleApiClient client = new GoogleApiClient.Builder(context) .addApi(Wearable.API) .build(); client.blockingConnect(); final NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(client).await(); for (Node node : nodes.getNodes()) { final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(client, node.getId(), Constants.UART.COMMAND, command.getBytes()).await(); if (!result.getStatus().isSuccess()) { Log.w(TAG, "Failed to send " + Constants.UART.COMMAND + " to " + node.getDisplayName()); } } client.disconnect(); } }).start(); }
/** * Sends the given message to all connected wearables. If the path is equal to {@link Constants.UART#DEVICE_DISCONNECTED} the service will be stopped afterwards. * @param path message path * @param message the message */ private void sendMessageToWearables(final @NonNull String path, final @NonNull String message) { if(mGoogleApiClient.isConnected()) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); for(Node node : nodes.getNodes()) { Logger.v(getLogSession(), "[WEAR] Sending message '" + path + "' to " + node.getDisplayName()); final MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, message.getBytes()).await(); if(result.getStatus().isSuccess()){ Logger.i(getLogSession(), "[WEAR] Message sent"); } else { Logger.w(getLogSession(), "[WEAR] Sending message failed: " + result.getStatus().getStatusMessage()); Log.w(TAG, "Failed to send " + path + " to " + node.getDisplayName()); } } if (Constants.UART.DEVICE_DISCONNECTED.equals(path)) stopService(); } }).start(); } else { if (Constants.UART.DEVICE_DISCONNECTED.equals(path)) stopService(); } }
private void sendMessageToDevice(final String key) { new Thread(new Runnable() { @Override public void run() { if (mGoogleApiClient == null) { mGoogleApiClient = (new com.google.android.gms.common.api.GoogleApiClient.Builder(ConfigDataListenerService.this)).addConnectionCallbacks(ConfigDataListenerService.this).addOnConnectionFailedListener(ConfigDataListenerService.this).addApi(Wearable.API).build(); } NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); if (nodes != null && nodes.getNodes() != null) { for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), key, null).await(); if (!result.getStatus().isSuccess()) { Log.e(TAG, "Error during sending message"); } else { Log.i(TAG, "Success!! sent to: " + node.getDisplayName()); } } } } }).start(); }
public void sendWearableMessage(String message, @Nullable final AsyncListener<Status> listener) { if (node != null && apiClient != null && apiClient.isConnected()) { Wearable.MessageApi.sendMessage(apiClient, node.getId(), WEAR_PATH, message.getBytes()).setResultCallback( new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) { if (listener != null) { Status status = sendMessageResult.getStatus(); if (status.isSuccess()) listener.onTaskComplete(status); else listener.onFailure(); } } } ); } else if (listener != null) listener.onFailure(); }
public void sendMessage(String command, byte[] data) { if (mNode == null) return; Wearable.MessageApi.sendMessage(mGoogleApiClient, mNode.getId(), command, data).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { /*if (!sendMessageResult.getStatus().isSuccess()) { }*/ } }); }
/** * Send the current step count to the phone. This does not require guarantee of message delivery. As, if * the message is not delivered, the count will get updated when second message gets delivered. * So, we are using messages api for passing the data that are usful at the current moment only. * <p> * <B>Note: </B> Messages will block the UI thread while sending. So, we should send messages in background * thread only. * * @param stepCount current step count. */ private void sendStepCountMessage(final String stepCount) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage( mGoogleApiClient, node.getId(), STEP_COUNT_MESSAGES_PATH, stepCount.getBytes()).await(); //check if the message is delivered? Log.d("Messages Api", result.getStatus().isSuccess() ? "Sent successfully" : "Sent failed."); } } }).start(); }
protected void fireMessage(final String command) { if (phoneNodeId == null) { //bad, we cannot send message } else { //fire the message to the first connected node in list Log.d(TAG, "Sending message to Node with ID: " + phoneNodeId); PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, phoneNodeId, command, null); messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { Status status = sendMessageResult.getStatus(); Log.d(TAG, "Status: " + status.toString()); if (status.getStatusCode() != WearableStatusCodes.SUCCESS) { Log.e(TAG,"Something go wrong during sending command... "+command); } else { Log.d(TAG,"Message sent successfully to node. "+command); } } }); } }
/** * Method to send a message to all other nodes. * * @param googleApiClient a connected googleApiClient * @param path path to send message to * @param data payload to send * @return Observable that emits whether the message was sent */ public static Observable<Boolean> getSendMessage(final GoogleApiClient googleApiClient, final String path, final byte[] data) { return NodeUtil.getRemoteNodeId(googleApiClient) .flatMap(new Func1<String, Observable<Boolean>>() { @Override public Observable<Boolean> call(final String nodeId) { return Observable.create(new Observable.OnSubscribe<Boolean>() { @Override public void call(final Subscriber<? super Boolean> subscriber) { Wearable.MessageApi.sendMessage(googleApiClient, nodeId, path, data).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { subscriber.onNext(sendMessageResult.getStatus().isSuccess()); subscriber.onCompleted(); } }); } }); } }); }
@Override public void onDestroy() { super.onDestroy(); if (googleApiClient != null && googleApiClient.isConnected()) { googleApiClient.disconnect(); } if (googleApiClient != null) { Wearable.MessageApi.removeListener(googleApiClient, this); } Log.d(TAG, "Stop Sensors"); stopMeasurement(); if (mPrefs.getBoolean("enable_wearG5", true)) { Log.d(TAG, "Start BT Collection Service"); stopBtService(); } }
private void initConnectionCallbacks() { mConnectionCallbacks=new GoogleApiClient.ConnectionCallbacks() { @Override public void onConnected(@Nullable Bundle bundle) { AddTextInterface("onConnected..."); initMessageListener(); Wearable.MessageApi.addListener(mApiClient, mMessageListener); sendMessageChecking(PublicConstants.START_ACTIVITY, ""); AddTextInterface("Sended START_ACTIVITY command"); } @Override public void onConnectionSuspended(int i) {AddTextInterface("Connection suspended");} }; }
public void run() { if (mWearableID == "") getMWearable(); if (mConnectedWearable != 1 && !Path.equals(PublicConstants.START_ACTIVITY) && !Path.equals(PublicConstants.STOP_ACTIVITY)) { //if not connected Log.e("SendMessage", "Not smartwatch connected"); OpenWearableApp(); }else { super.run(); if (PendingResultSend != null) { MessageApi.SendMessageResult result = PendingResultSend.await(); if (!result.getStatus().isSuccess()) Log.e("sendMessage", "ERROR: failed to send Message: " + result.getStatus()); } else AddTextInterface("No smartwatch connected", Toast.LENGTH_LONG); } }
public void sendMessage(String message) { if(mNode != null && mGoogleApiClient != null) { Wearable.MessageApi.sendMessage(mGoogleApiClient, mNode.getId(), Constants.MYCROFT_QUERY_MESSAGE_PATH, message.getBytes()).setResultCallback( new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) { if (!sendMessageResult.getStatus().isSuccess()) { showToast("Message Failed"); } else { showToast("Message Sent"); } } } ); } else { showToast("Unable To Send Message"); } }
@Override public void onConnected(Bundle bundle) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult connectedNodesResult = Wearable.NodeApi.getConnectedNodes(mApiClient).await(); String tag = MyWatchFace.class.getSimpleName(); for (Node node : connectedNodesResult.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage( mApiClient, node.getId(), "/RequestBatteryLevel", "RequestBatteryLevel".getBytes()) .await(); if (result.getStatus().isSuccess()) { Log.d(tag, "バッテリーレベル要求の送信に成功"); } else { Log.d(tag, "バッテリーレベル要求の送信に失敗 (" + result.getStatus().getStatusMessage() + ")"); } } } }).start(); }
public static void sendMessage(final GoogleApiClient client, final String command, final byte[] message, final ResultCallback<MessageApi.SendMessageResult> listener) { new Thread(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( client ).await(); for(Node node : nodes.getNodes()) { Log.i(TAG, "sending to " + node.getId() + ", command: " + command); PendingResult<MessageApi.SendMessageResult> pR = Wearable.MessageApi.sendMessage(client, node.getId(), command, message); if (listener != null) pR.setResultCallback(listener); } } }).start(); }
@Override protected void onStop() { Log.i(TAG, "onStop"); if (mHandler != null) mHandler.removeCallbacksAndMessages(null); if (mVibrator != null) mVibrator.cancel(); if (mNfcAdapter != null) { mNfcAdapter.disableReaderMode(this); } if ((mGoogleApiClient != null) && (mGoogleApiClient.isConnected())) { Wearable.MessageApi.removeListener(mGoogleApiClient, this); mGoogleApiClient.disconnect(); } if (mRootTools != null) mRootTools.executeScripts(false, 15000); if ((mWakeLock != null) && (mWakeLock.isHeld())) mWakeLock.release(); finish(); super.onStop(); }
@Override public void onConnectionFailed(ConnectionResult result) { if (mResolvingError) { // Already attempting to resolve an error. return; } else if (result.hasResolution() && mActivity != null) { try { mResolvingError = true; result.startResolutionForResult(mActivity, 1000); } catch (IntentSender.SendIntentException e) { // There was an error with the resolution intent. Try again. mGoogleApiClient.connect(); } } else { Log.e(TAG, "Connection to Google API client has failed"); mResolvingError = false; if (mListener != null) mListener.onDataUpdated(); Wearable.MessageApi.removeListener(mGoogleApiClient, this); Wearable.DataApi.removeListener(mGoogleApiClient, this); } }
/** * Sends an asynchronous message to the node with the given {@code nodeId} through {@code * path}. If the {@code callback} is null, then a default callback will be used that provides a * feedback to the caller using the {@link DataConsumer#onSendMessageResult}, in which case, * the status of the result will be made available. Callers may decide to provide their own * {@code callback} to be used instead. This variant receives the message in an array of bytes. */ public void sendMessage(String nodeId, String path, @Nullable byte[] bytes, @Nullable final ResultCallback<? super MessageApi.SendMessageResult> callback) { assertApiConnectivity(); Wearable.MessageApi.sendMessage(mGoogleApiClient, nodeId, path, bytes).setResultCallback( new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { if (!sendMessageResult.getStatus().isSuccess()) { Log.e(TAG, "Failed to send message, statusCode: " + sendMessageResult .getStatus().getStatusCode()); } if (callback == null) { for (DataConsumer consumer : mDataConsumers) { consumer.onSendMessageResult(sendMessageResult.getStatus() .getStatusCode()); } } else { callback.onResult(sendMessageResult); } } }); }
private void sendMessage(final String path, final String message) { new Thread( new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( mApiClient ).await(); for(Node node : nodes.getNodes()) { MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage( mApiClient, node.getId(), path, message.getBytes() ).await(); } runOnUiThread( new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "m to phon" + message, Toast.LENGTH_SHORT).show(); } }); } }).start(); }
private void sendAsyncMessage(final String path, final String message) { Wearable.NodeApi.getConnectedNodes( mApiClient ).setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(@NonNull NodeApi.GetConnectedNodesResult nodes) { for(Node node : nodes.getNodes()) { Wearable.MessageApi.sendMessage( mApiClient, node.getId(), path, message.getBytes() ).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(@NonNull MessageApi.SendMessageResult sendMessageResult) { if (sendMessageResult.getStatus().isSuccess()){ showToastMT(message); } } }); } } }); }
public void run() { if ((objectArray.length / 1024) > 100) { throw new RuntimeException("Object is too big to push it via Google Play Services"); } GoogleApiClient googleApiClient = SendWearManager.getInstance(context); googleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS); NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await(); if(nodes != null) { for (Node node : nodes.getNodes()) { MessageApi.SendMessageResult result; result = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), path, objectArray).await(); if (!result.getStatus().isSuccess()) { Log.v(TAG, "ERROR: failed to send Message via Google Play Services"); } Log.d(TAG, "run send:"); } } }
@Override public boolean onRequest(final Intent request, final Intent response) { String nodeId = WearUtils.getNodeId(getServiceID(request)); getManager().sendMessageToWear(nodeId, WearConst.DEVICE_TO_WEAR_CANCAS_DELETE_IMAGE, "", new WearManager.OnMessageResultListener() { @Override public void onResult(final MessageApi.SendMessageResult result) { if (result.getStatus().isSuccess()) { setResult(response, DConnectMessage.RESULT_OK); } else { MessageUtils.setIllegalDeviceStateError(response); } sendResponse(response); } @Override public void onError() { MessageUtils.setIllegalDeviceStateError(response); sendResponse(response); } }); return false; }
/** * メッセージをWearに送信する. * * @param dest 送信先のWearのnodeId * @param action メッセージのアクション * @param message メッセージ * @param listener メッセージを送信した結果を通知するリスナー */ public void sendMessageToWear(final String dest, final String action, final String message, final OnMessageResultListener listener) { sendMessageToWear(new Runnable() { @Override public void run() { NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes(mGoogleApiClient).await(); MessageApi.SendMessageResult result = null; for (Node node : nodes.getNodes()) { if (node.getId().indexOf(dest) != -1) { result = Wearable.MessageApi.sendMessage( mGoogleApiClient, node.getId(), action, message.getBytes()).await(); } } if (result != null) { if (listener != null) { listener.onResult(result); } } else { if (listener != null) { listener.onError(); } } } }); }
public void sendMessage(final String destinationId, final String path, final String data) { mExecutorService.execute(new Runnable() { @Override public void run() { GoogleApiClient client = mGoogleApiClient; if (!client.isConnected()) { ConnectionResult connectionResult = client.blockingConnect(30, TimeUnit.SECONDS); if (!connectionResult.isSuccess()) { if (BuildConfig.DEBUG) { Log.e("WEAR", "Failed to connect google play service."); } return; } } MessageApi.SendMessageResult result = Wearable.MessageApi.sendMessage(client, destinationId, path, data.getBytes()).await(); if (!result.getStatus().isSuccess()) { if (BuildConfig.DEBUG) { Log.e("WEAR", "Failed to send a sensor event."); } } } }); }
private void controlMeasurementInBackground(final String path, byte[] extra) { if (validateConnection()) { List<Node> nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await().getNodes(); Log.d(TAG, "Sending to nodes: " + nodes.size()); for (Node node : nodes) { Log.i(TAG, "add node " + node.getDisplayName()); Wearable.MessageApi.sendMessage( googleApiClient, node.getId(), path, extra ).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { Log.d(TAG, "controlMeasurementInBackground(" + path + "): " + sendMessageResult.getStatus().isSuccess()); } }); } } else { Log.w(TAG, "No connection possible"); } }
@Override public void sendMessageAll(@Nullable final byte[] payload, @Nullable String path, @Nullable final SendResultListener<MessageApi.SendMessageResult> listener) { if (path == null) { path = DEFAULT_MESSAGE_API_PATH; } final String finalPath = path; mHelper.getNodes(new NodeChangeListener() { @Override public void onReceiveNodes(List<Node> nodes) { for (Node node : nodes) { sendMessage(node, payload, finalPath, listener); } } }); }
@Override public void sendMessageAllAsync(@Nullable final byte[] payload, @Nullable String path, @Nullable final SendResultListener<MessageApi.SendMessageResult> listener) { if (path == null) { path = DEFAULT_MESSAGE_API_PATH; } final String finalPath = path; mHelper.getNodes(new NodeChangeListener() { @Override public void onReceiveNodes(List<Node> nodes) { for (Node node : nodes) { sendMessageAsync(node, payload, finalPath, listener); } } }); }
@Override public void onMessageReceived(MessageEvent event) { Log.d(TAG, "onMessageReceived: " + event); switch (event.getPath()) { case Consts.Paths.GAME_SHOT: Log.d(TAG, "onMessageReceived: SHOT TIME"); GameState.getInstance().setIsShotTime(true); mGameScreen.updateScreen(isAmbient()); break; case Consts.Paths.GAME_STOP: Log.d(TAG, "onMessageReceived: Game has stopped"); GameState.getInstance().stop(); updateDisplay(); Message.sendReady(this); break; case Consts.Paths.GAME_FINISH: Log.i(TAG, "onMessageReceived: Game has finished"); Wearable.DataApi.removeListener(mGoogleApiClient, this); Wearable.MessageApi.removeListener(mGoogleApiClient, this); finish(); break; } }
/** * send message * you don't have to send anything * * @param node 送信先node * @param payload * @param path * @param listener */ public void sendMessage(@NonNull final Node node, @Nullable final byte[] payload, @NonNull final String path, @Nullable final SendResultListener<MessageApi.SendMessageResult> listener) { final PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(mGoogleApiClient, node.getId(), path, payload); messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(final MessageApi.SendMessageResult sendMessageResult) { Log.d(TAG, "Status: " + sendMessageResult.getStatus()); if (listener != null) { listener.onResult(sendMessageResult); } } }); }
@Override public void onVisibilityChanged(boolean visible) { super.onVisibilityChanged(visible); if (visible) { registerReceiver(); mGoogleApiClient.connect(); // Update time zone in case it changed while we weren't visible. mTime.clear(TimeZone.getDefault().getID()); mTime.setToNow(); } else { unregisterReceiver(); if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { Wearable.DataApi.removeListener(mGoogleApiClient, this); Wearable.NodeApi.removeListener(mGoogleApiClient, this); Wearable.MessageApi.removeListener(mGoogleApiClient, this); mGoogleApiClient.disconnect(); mGoogleApiClient.disconnect(); } } // Whether the timer should be running depends on whether we're visible (as well as // whether we're in ambient mode), so we may need to start or stop the timer. updateTimer(); }
/** * Will synchronize a preference to the other device by using the Wearable MessageAPI. * Note: synchronization might fail even if this method returns true. Don't rely on this mechanism. * Don't use this method but the above wrapper methods instead! * * @param googleApiClient connected instance of the Google API Client to send a message * @param nodeID node id of the other device * @param prefKey key of the preference that should be synchronized * @param type lowercase type of the preference (e.g. "string", "long", or "boolean") * @param newValue new value of the preference encoded as byte array * @return true if the message was sent successfully. This does not guarantee that the other device has received the msg */ public static boolean syncPref(GoogleApiClient googleApiClient, String nodeID, final String prefKey, String type, byte[] newValue) { if(googleApiClient == null || !googleApiClient.isConnected()) { Log.e(LOGTAG, "syncPref: GoogleApiClient is not connected!"); return false; } if(nodeID == null || nodeID.equals("")) { Log.e(LOGTAG, "syncPref: Node ID is invalid!"); return false; } // Send it to the handheld device: Wearable.MessageApi.sendMessage(googleApiClient, nodeID, "/syncPref/" + type + "/" + prefKey, newValue).setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { if (!sendMessageResult.getStatus().isSuccess()) Log.e(LOGTAG, "syncPref: Failed to sync preference ("+prefKey+") to the handheld: " + sendMessageResult.toString()); else Log.d(LOGTAG, "syncPref: Synced preference "+prefKey+" (messageID=" + sendMessageResult.getRequestId() + ")!"); } }); return true; }