Charles LAU
Charles LAU

Reputation: 281

Issue with reading data from socket, UI freeze in Android application

I am currently working on an android application which requires reading data from bluetooth socket. The code I am using is as follows:

runOnUiThread(new Runnable() {
    public void run()
    {
        try{
            ReadData();
    }catch(Exception ex){}
    }
});
public void ReadData() throws Exception{
    try {
        b1 = new StringBuilder();
        stream = socket.getInputStream();
        int intch;
        String output = null;
        String k2 = null;
        byte[] data = new byte[10];
        // read data from input stream if the end has not been reached
        while ((intch = stream.read()) != -1) {
            byte ch = (byte) intch;
            b1.append(ByteToHexString(ch) +"/");
            k++;
            if(k == 20) // break the loop and display the output
            {
                output = decoder.Decode(b1.toString());
                textView.setText(output);
                k=0;
                break;
            }
        }
        // close the input stream, reader and socket
        if (stream != null) {
            try {stream.close();} catch (Exception e) {}
            stream = null;
        }
        if (socket != null) {
            try {socket.close();} catch (Exception e) {}
            socket = null;
        }
    } catch (Exception e) {
    }
}

However, when I run the application on android device, the UI doesn't update automatically and it keeps freezing. Does anyone know how to resolve the UI freeze issue? I would like to display the data on UI dynamically rather than display the data after finishing the loop.

Thanks for any helps in advance.

Regards,

Charles

Upvotes: 0

Views: 1269

Answers (3)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Try this,

How it works.

1. When Android Application starts you are on the UI Thread. Doing any Process intensive work on this thread will make your UI unresponsive.

2. Its always advice to keep UI work on UI Thread and Non-UI work on Non-UI Thread. But from HoneyComb version in android it became a law.

Whats the Problem in your code.

1. You are reading the data on the UI thread, making it wait to finish reading.. here in this line...while ((intch = stream.read()) != -1)).

How to Solve it:

1. Use a separate Non-UI thread, and to put the value back to the UI thread use Handler.

2. Use the Handler class. Handler creates a reference to the thread on which it was created. This will help you put the work done on the Non-UI thread back on the UI thread.

3. Or use AsyncTask provided in android to Synchronize the UI and Non-UI work,which does work in a separate thread and post it on UI.

Upvotes: 0

Chris Stratton
Chris Stratton

Reputation: 40407

You should be running the ReadData() method on a non-UI thread, and then once data is available use the runOnUIthread mechanism to run just the resulting update of the textView on the UI thread.

Upvotes: 0

GETah
GETah

Reputation: 21449

On InputStream.read() java says:

This method blocks until input data is available

Your UI is blocking because you are reading from the socket on the UI thread. You should definitly have another thread that reads the data from the socket and passes the results to the UI for dynamic updates.

Upvotes: 1

Related Questions