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); } } }); } }
public static void sendSelectedImage(final Uri uri, ImageView imageView) { final Context context = imageView.getContext(); // show image locally Glide.with(context).load(uri).into(imageView); // send to wearable FileTransfer fileTransferHighLevel = new FileTransfer.Builder() .setOnChannelOutputStreamListener(new FileTransfer.OnChannelOutputStreamListener() { @Override public void onOutputStreamForChannelReady(int statusCode, Channel channel, final OutputStream outputStream) { if (statusCode != WearableStatusCodes.SUCCESS) { Logger.e("Failed to open a channel, status code: " + statusCode); return; } sendFile(context, uri, outputStream); } }).build(); fileTransferHighLevel.requestOutputStream(); }
@Override protected com.google.android.gms.common.api.Status doInBackground(SoundProfile... params) { final List<Node> connectedNodes = Wearable.NodeApi.getConnectedNodes(googleApiClient).await().getNodes(); if (connectedNodes.isEmpty()) { return new com.google.android.gms.common.api.Status(WearableStatusCodes.ERROR); } com.google.android.gms.common.api.Status status = null; for (Node node : connectedNodes) { if (status == null || !status.isSuccess()) { status = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), params[0].name(), null).await().getStatus(); } } return status; }
/** * Initiates the transfer of a file to the target node. Using this method, a file can be * transferred without any additional work on the receiver end although the receiver can * register a {@link DataConsumer#onFileReceivedResult(int, String, File, String)} * to be notified when the transfer is completed or when it fails. * Using {@link DataConsumer#onSendFileResult(int, String)}, the sender node can also * learn about the status of the file transfer request. */ public void startTransfer() { assertFileTransferParams(); final Uri uri = Uri.fromFile(mFile); String path = buildPath(mTargetName, mRequestId, mFile.length()); final GmsWear gmsWear = getInstance(); if (mNode == null) { return; } gmsWear.openChannel(mNode, path, new OnChannelReadyListener() { @Override public void onChannelReady(int statusCode, Channel channel) { if (statusCode != WearableStatusCodes.SUCCESS) { Log.e(TAG, "transfer(): Failed to open channel; status code= " + statusCode); if (mFileTransferResultListener != null) { mFileTransferResultListener.onFileTransferStatusResult(statusCode); } return; } gmsWear.sendFile(mRequestId, channel, uri, 0, -1, new ResultCallback<Status>() { @Override public void onResult(Status status) { if (status.getStatusCode() != WearableStatusCodes.SUCCESS) { Log.e(TAG, "transfer(): Failed to send file; status code= " + status .getStatusCode()); } if (mFileTransferResultListener != null) { mFileTransferResultListener .onFileTransferStatusResult(status.getStatusCode()); } } }); } }); }
private void fireMessage(final byte[] data, final String path,final int retryCount) { PendingResult<NodeApi.GetConnectedNodesResult> nodes = Wearable.NodeApi.getConnectedNodes(googleApiClient); nodes.setResultCallback(new ResultCallback<NodeApi.GetConnectedNodesResult>() { @Override public void onResult(NodeApi.GetConnectedNodesResult result) { for (int i = 0; i < result.getNodes().size(); i++) { Node node = result.getNodes().get(i); Log.v(TAG, "Phone firing message with path : " + path); PendingResult<MessageApi.SendMessageResult> messageResult = Wearable.MessageApi.sendMessage(googleApiClient, node.getId(), path, data); messageResult.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() { @Override public void onResult(MessageApi.SendMessageResult sendMessageResult) { Status status = sendMessageResult.getStatus(); Timber.d("Status: " + status.toString()); if (status.getStatusCode() != WearableStatusCodes.SUCCESS) { if(!status.isSuccess()) { if (retryCount > 5) return; soundThreadHandler.postDelayed(new Runnable() { @Override public void run() { fireMessage(data, path, retryCount + 1); } }, 1000 * retryCount); } } if (path.equals(CommonIdentifiers.P2W_CHANGE_SETTINGS)) { Intent messageIntent = new Intent(); messageIntent.setAction(Intent.ACTION_SEND); messageIntent.putExtra("status", status.getStatusCode()); LocalBroadcastManager.getInstance(WearMessageListenerService.this).sendBroadcast(messageIntent); } } }); } } }); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wear_activity); mGmsWear = GmsWear.getInstance(); mMsgTextView = (TextView) findViewById(R.id.tv_msg); mMsgTextView.setKeepScreenOn(true); mImageView = (ImageView) findViewById(R.id.imageView); mMsgOneButton = (Button) findViewById(R.id.btn_msg_one); mMsgTwoButton = (Button) findViewById(R.id.btn_msg_two); mSyncButton = (Button) findViewById(R.id.btn_sync); mDataConsumer = new AbstractDataConsumer() { @Override public void onMessageReceived(MessageEvent messageEvent) { String msg = new String(messageEvent.getData()); if (messageEvent.getPath().equals(PATH_MSG_ONE)) { setTextAsync(mMsgTextView, msg + PATH_MSG_ONE); } else if (messageEvent.getPath().equals(PATH_MSG_TWO)) { setTextAsync(mMsgTextView, msg + PATH_MSG_TWO); } } @Override public void onDataChanged(DataEvent event) { DataItem item = event.getDataItem(); final DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap(); if (event.getType() == DataEvent.TYPE_CHANGED) { String syncString = dataMap.getString(SYNC_KEY); setTextAsync(mMsgTextView, syncString); } else if (event.getType() == DataEvent.TYPE_DELETED) { Logger.d("DataItem Deleted" + event.getDataItem().toString()); } } @Override public void onFileReceivedResult(int statusCode, String requestId, File savedFile, String originalName) { Logger.d( "File Received: status=%d, requestId=%s, savedLocation=%s, originalName=%s", statusCode, requestId, savedFile.getAbsolutePath(), originalName); Glide.with(getApplicationContext()).load(savedFile).into(mImageView); } @Override public void onInputStreamForChannelOpened(int statusCode, String requestId, final Channel channel, final InputStream inputStream) { if (statusCode != WearableStatusCodes.SUCCESS) { Logger.e("onInputStreamForChannelOpened(): " + "Failed to get input stream"); return; } Logger.d("Channel opened for path: " + channel.getPath()); setImageFromInputStream(inputStream, mImageView); } }; checkPermissions(); }