@Override public void onChannelOpened(Channel channel) { super.onChannelOpened(channel); Log.v("channel", "openned"); mDataChannel = channel; mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(Wearable.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); mGoogleApiClient.connect(); }
private void createChannel() { Wearable.ChannelApi.openChannel(mGoogleApiClient, mNode.getId(), "data_channel").setResultCallback(new ResultCallback<ChannelApi.OpenChannelResult>() { @Override public void onResult(@NonNull ChannelApi.OpenChannelResult openChannelResult) { Channel channel = openChannelResult.getChannel(); channel.getOutputStream(mGoogleApiClient).setResultCallback(new ResultCallback<Channel.GetOutputStreamResult>() { @Override public void onResult(@NonNull Channel.GetOutputStreamResult getOutputStreamResult) { mOutputStream = getOutputStreamResult.getOutputStream(); Log.v("channel", "created"); channelCreated=true; } }); } }); }
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(); }
public void sendFile(final String requestId, Channel channel, Uri file, long startOffset, long length, ResultCallback<Status> callback) { channel.addListener(mGoogleApiClient, new FileChannelListener()); PendingResult<Status> result = channel.sendFile(mGoogleApiClient, file, startOffset, length); if (callback == null) { callback = new ResultCallback<Status>() { @Override public void onResult(Status status) { for (DataConsumer consumer : mDataConsumers) { consumer.onSendFileResult(status.getStatusCode(), requestId); } } }; } result.setResultCallback(callback); }
/** * Initiates opening a channel to a nearby node. When done, it will call the {@code listener} * and passes the status code of the request and the channel that was opened. Note that if the * request was not successful, the channel passed to the listener will be {@code null}. <br/> * <strong>Note:</strong> It is the responsibility of the caller to close the channel and the * stream when it is done. * * @param node The node to which a channel should be opened. Note that {@code * node.isNearby()} should return {@code true} * @param path The path used for opening a channel * @param listener The listener that is called when this request is completed. */ public void openChannel(Node node, String path, final FileTransfer.OnChannelReadyListener listener) { if (node.isNearby()) { Wearable.ChannelApi.openChannel( mGoogleApiClient, node.getId(), path).setResultCallback( new ResultCallback<ChannelApi.OpenChannelResult>() { @Override public void onResult(ChannelApi.OpenChannelResult openChannelResult) { int statusCode = openChannelResult.getStatus().getStatusCode(); Channel channel = null; if (openChannelResult.getStatus().isSuccess()) { channel = openChannelResult.getChannel(); } else { Log.e(TAG, "openChannel(): Failed to get channel, status code: " + statusCode); } listener.onChannelReady(statusCode, channel); } }); } else { Log.e(TAG, "openChannel(): Node should be nearby, you have: " + node); } }
/** * Clients can register to {@link DataConsumer#onChannelClosed(Channel, int, int)}. */ void onChannelClosed(Channel channel, int closeReason, int appSpecificErrorCode) { for (DataConsumer consumer : mDataConsumers) { consumer.onChannelClosed(channel, closeReason, appSpecificErrorCode); } }
/** * 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()); } } }); } }); }
public TestThread(Channel.GetInputStreamResult getInputStreamResult) { this.getInputStreamResult = getInputStreamResult; }
@Override public void onChannelOpened(Channel channel) { super.onChannelOpened(channel); }
@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(); }
@Override public void onChannelOpened(Channel channel) { //no-op }
@Override public void onChannelClosed(Channel channel, int closeReason, int appSpecificErrorCode) { //no-op }
@Override public void onInputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { //no-op }
@Override public void onOutputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { //no-op }
@Override public void onInputStreamForChannelOpened(int statusCode, String requestId, Channel channel, InputStream inputStream) { //no-op }
@Override public void onOutputStreamForChannelReady(int statusCode, Channel channel, OutputStream outputStream) { //no-op }
/** * Opens an {@link OutputStream} to a nearby node. To do this, this method first makes an * attempt to open a channel to the target node using the {@code path} that is provided. If * successful, then it opens an {@link OutputStream} using that channel. Finally, it calls the * {@code listener} when the {@link OutputStream} is available. On the target node, clients * should register a * {@link DataConsumer#onInputStreamForChannelOpened(int, String, Channel, InputStream)} * to be notified of the availability of an {@link InputStream} to handle the incoming bytes. * <p> * <p>Caller should register a {@link FileTransfer.OnChannelOutputStreamListener} * listener to be notified of the status of the request and to obtain a reference to the * {@link OutputStream} that is opened upon successful execution. * * @param node The node to open a channel for data transfer. Note that this node should be * nearby otherwise this method will return immediately without performing any * additional tasks. * @param path The path that will be used to open a channel for transfer. * @param listener The listener that will be notified of the status of this request. Upon a * successful execution, this listener will receive a pointer to the {@link * OutputStream} that * was opened. */ public void getOutputStreamViaChannel(Node node, String path, final FileTransfer.OnChannelOutputStreamListener listener) { if (!node.isNearby()) { Log.e(TAG, "getOutputStreamViaChannel(): Node should be nearby, you have: " + node); } else { Wearable.ChannelApi.openChannel( mGoogleApiClient, node.getId(), path).setResultCallback( new ResultCallback<ChannelApi.OpenChannelResult>() { @Override public void onResult(ChannelApi.OpenChannelResult openChannelResult) { if (openChannelResult.getStatus().isSuccess()) { final Channel channel = openChannelResult.getChannel(); channel.addListener(mGoogleApiClient, new FileChannelListener()); channel.getOutputStream(mGoogleApiClient).setResultCallback( new ResultCallback<Channel.GetOutputStreamResult>() { @Override public void onResult( Channel.GetOutputStreamResult getOutputStreamResult) { if (getOutputStreamResult.getStatus().isSuccess()) { OutputStream outputStream = getOutputStreamResult.getOutputStream(); listener.onOutputStreamForChannelReady( getOutputStreamResult.getStatus() .getStatusCode(), channel, outputStream); } else { closeChannel(channel); listener.onOutputStreamForChannelReady( getOutputStreamResult.getStatus() .getStatusCode(), null, null); } } }); } else { listener.onOutputStreamForChannelReady( openChannelResult.getStatus().getStatusCode(), null, null); } } }); } }
/** * Closes the {@code channel} if it is not {@code null}. */ public void closeChannel(Channel channel) { if (channel != null) { channel.close(mGoogleApiClient); } }
/** * Clients can register to {@link DataConsumer#onInputClosed(Channel, int, int)}. */ void onInputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { for (DataConsumer consumer : mDataConsumers) { consumer.onInputClosed(channel, closeReason, appSpecificErrorCode); } }
/** * Clients can register to {@link DataConsumer#onOutputClosed(Channel, int, int)}. */ void onOutputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { for (DataConsumer consumer : mDataConsumers) { consumer.onOutputClosed(channel, closeReason, appSpecificErrorCode); } }
ReceivedFileResultCallback(String requestId, File outFile, long size, Channel channel) { this.requestId = requestId; this.outFile = outFile; this.size = size; this.channel = channel; }
@Override public void onChannelOpened(Channel channel) { }
@Override public void onChannelClosed(Channel channel, int closedReason, int appSpecificErrorCode) { WearUtil.logD(TAG, "Channel Closed"); }
@Override public void onInputClosed(Channel channel, int closedReason, int appSpecificErrorCode) { }
@Override public void onOutputClosed(Channel channel, int closedReason, int appSpecificErrorCode) { WearUtil.logD(TAG, "onOutputClosed(): Output closed so closing channel..."); closeChannel(channel); }
@Override public void onChannelOpened(Channel channel) { super.onChannelOpened(channel); mGmsWear.onChannelOpened(channel); }
@Override public void onChannelClosed(Channel channel, int closeReason, int appSpecificErrorCode) { super.onChannelClosed(channel, closeReason, appSpecificErrorCode); mGmsWear.onChannelClosed(channel, closeReason, appSpecificErrorCode); }
@Override public void onInputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { super.onInputClosed(channel, closeReason, appSpecificErrorCode); mGmsWear.onInputClosed(channel, closeReason, appSpecificErrorCode); }
@Override public void onOutputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { super.onOutputClosed(channel, closeReason, appSpecificErrorCode); mGmsWear.onOutputClosed(channel, closeReason, appSpecificErrorCode); }
@Override public void onChannelOpened(Channel channel) { super.onChannelOpened(channel); Log.d(TAG, "onChannelOpened() ...") ; }
@Override public void onChannelClosed(Channel channel, int closeReason, int appSpecificErrorCode) { super.onChannelClosed(channel, closeReason, appSpecificErrorCode); Log.d(TAG, "onChannelClosed() ...") ; }
@Override public void onInputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { super.onInputClosed(channel, closeReason, appSpecificErrorCode); Log.d(TAG, "onInputClose() ...") ; }
@Override public void onOutputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { super.onOutputClosed(channel, closeReason, appSpecificErrorCode); Log.d(TAG, "onOutputClosed() ...") ; }
@Override @CallSuper public void onChannelOpened(Channel channel) { }
@Override @CallSuper public void onChannelClosed(Channel channel, int closeReason, int appSpecificErrorCode) { }
@Override @CallSuper public void onInputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { }
@Override @CallSuper public void onOutputClosed(Channel channel, int closeReason, int appSpecificErrorCode) { }
/** * Called when a channel is closed. */ void onChannelClosed(Channel channel, int closeReason, int appSpecificErrorCode);
/** * Called when InputStream associated with the channel is close. */ void onInputClosed(Channel channel, int closeReason, int appSpecificErrorCode);
/** * Called when the output stream associated with the channel is closed. */ void onOutputClosed(Channel channel, int closeReason, int appSpecificErrorCode);