简体   繁体   English

当我调用bufferedReader.readLine()时,我的Android应用程序冻结了;

[英]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 . 我为Android手机和Android Wear创建了一个新的Android Studio项目,以测试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. 看来,在我的项目中一切正常,除了在onChannelOpened方法内部, onChannelOpened不冻结应用程序就无法从InputStream读取数据。

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 日志打印: 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. 日志打印: D/MainActivity﹕ onChannelOpened: onResult ,仅此而已。

I started the debugger and now know that the error must be in this line: line = bufferedReader.readLine(); 我启动了调试器,现在知道错误一定在此行中: line = bufferedReader.readLine(); . But there is no exception thrown or anything else. 但是没有抛出异常或其他任何异常。 The debugger even shows me that the buffer has the right values: 调试器甚至向我展示了缓冲区具有正确的值:

调试器显示正确的值

I am trying to send "hello from your smartwatch". 我正在尝试发送“来自您的smartwatch的问候”。 However, the app seems to freeze. 但是,该应用似乎冻结了。 Does anyone know why? 有人知道为什么吗?

您需要在发送方关闭流,否则接收方将不知道是否有更多数据通过该通道流失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM