Issue with reading data from socket, UI freeze in Android application
<p>I am currently working on an android application which requires reading data from bluetooth socket. The code I am using is as follows:</p>

<pre><code>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) {
    }
}
</code></pre>

<p>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.</p>

<p>Thanks for any helps in advance.</p>

<p>Regards,</p>

<p>Charles</p>
