javaandroidbufferedreaderwear-oschannel-api

My Android app is freezing when I call bufferedReader.readLine();


I created a new Android Studio project for an Android phone and an Android Wear to test the ChannelApi. It seems that everything works fine in my project, except that inside the onChannelOpened method I cannot read from InputStream without freezing the app.

This is how I send the message from the Wearable (which seems to work):

Wearable.ChannelApi.openChannel(googleApiClient, node.getId(), channelIdentifier).setResultCallback(new ResultCallback<ChannelApi.OpenChannelResult>() {
    @Override
    public void onResult(ChannelApi.OpenChannelResult openChannelResult) {
        final Channel channel = openChannelResult.getChannel();
        channel.getOutputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetOutputStreamResult>() {
        @Override
        public void onResult(final Channel.GetOutputStreamResult getOutputStreamResult) {
        final String message = "hello from your smartwatch";
        try {
            getOutputStreamResult.getOutputStream().write(message.getBytes());
            Log.d(TAG, "sendMessageToNode: onResult: onResult: Message sent: " + message);
        }
        // ...

The log prints: D/MainActivity﹕ sendMessageToNode: onResult: onResult: Message sent: hello from your smartwatch

On the smartphone side I try to read the message as follows:

@Override
public void onChannelOpened(final Channel channel) {
    channel.getInputStream(googleApiClient).setResultCallback(new ResultCallback<Channel.GetInputStreamResult>() {
        @Override
        public void onResult(Channel.GetInputStreamResult getInputStreamResult) {
            Log.d(TAG, "onChannelOpened: onResult");

            String message = "";
            InputStream inputStream = null;
            BufferedReader bufferedReader = null;
            try {
                inputStream = getInputStreamResult.getInputStream();
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                String line = "";
                //while ((line = bufferedReader.readLine()) != null) {
                while (true) {
                    // Next line is freezing the app
                    line = bufferedReader.readLine();
                    Log.d(TAG, "onChannelOpened: onResult: line: " + line);
                    message += line + "\n";
                    if (line == null) break;
                }
                Log.d(TAG, "onChannelOpened: onResult: Received the following message: \" + message");
                // ...

The log prints: D/MainActivity﹕ onChannelOpened: onResult but that's it.

I started the debugger and now know that the error must be in this line: line = bufferedReader.readLine();. But there is no exception thrown or anything else. The debugger even shows me that the buffer has the right values:

Debugger shows right values

I am trying to send "hello from your smartwatch". However, the app seems to freeze. Does anyone know why?


Solution

  • You needed to close the stream on the sender side otherwise the receiver wouldn't know if more data would be coming down through the channel or not.