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; } }); } }); }
/** * 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); } }
/** * 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); } } }); } }
@Override public PendingResult<Status> addListener(GoogleApiClient client, ChannelApi.ChannelListener listener) { Log.d(TAG, "unimplemented Method: addListener"); return null; }
@Override public PendingResult<Status> removeListener(GoogleApiClient client, ChannelApi.ChannelListener listener) { Log.d(TAG, "unimplemented Method: removeListener"); return null; }