0

I have a thread called DataInputstreamThread. DataInputstreamThread gets input from a bluetooth device.

I want to add the processed data from the inputstream to a textbox by using a runOnUiThread. But this doesn't work. runOnUiThread gets skipped everytime.

Code::

public void getDataFromInputStream() {

    DataInputstreamThread = new Thread(new Runnable() {
        public void run() {


            threadFlag = connectDevice();
            while (threadFlag) {
                try {
                    if (inputStream == null) {
                        inputStream = bluetoothSocket.getInputStream();
                    }

                    bytesAvailable = inputStream.available();
                    if (bytesAvailable > 0) {
                        byte[] packetBytes = new byte[bytesAvailable];
                        inputStream.read(packetBytes);

                        for (int i = 0; i < bytesAvailable; i++) {

                            if (packetBytes[i] != 13) {
                                temp = new String(packetBytes);

                            } else {
                                delimiter = true;
                            }

                        }

                        fin += temp;//I have a breakpoint here, and i know this is executed


                        activity.runOnUiThread(new Runnable() {
                            public void run() {
                             notifyObservers(fin);//I have breakpoint here, and this line is not executed.
                            }
                        });

                        if (delimiter) {

                            fin = "";
                            delimiter = false;
                        }

                    }


                } catch (Exception e) {

                }

            }

            nullify();
        }

    });
    DataInputstreamThread.start();
}

So why is the runOnUiThread not being executed?

rene
  • 41,474
  • 78
  • 114
  • 152
MeesterPatat
  • 2,671
  • 9
  • 31
  • 54
  • Meanwhile, what is your UI thread doing? `runOnUiThread()` from a non-UI thread posts an event to the event queue. If your UI thread is blocked doing something else, no events are processed. – laalto Sep 09 '13 at 09:28
  • @laalto Honestly i have no idea if the UI thread is doing something else in the background, i'm not the only person developing the app, so maby someone else's code is doing something on the UI thread. Is there a convenient way to see if the UI thread is working on something else? – MeesterPatat Sep 09 '13 at 09:44
  • @laalto But then again: If my call to the UIthread is being placed in the event queue, i still should see a jump to the breakpoint inside the runOnuiThread? – MeesterPatat Sep 09 '13 at 09:51
  • Yes, when the event is processed, the Runnable is executed and you should end up in the breakpoint. What I was after is asking whether something prevents the UI thread event loop from running. (However, I don't trust debugger breakpoints fully. I use logging where I need to know exactly where the code is going.) – laalto Sep 09 '13 at 09:57

2 Answers2

3

This does not directly answer the question you asked, but it explains why you cannot find the cause of the problem.

This:

catch (Exception e) {

}

is destroying any hope you might have in finding the bug. You swallow the exception, so you don't see the error that runOnUiThread might throw. You are left to do guesswork.

Do something useful there. At the very least, output the exception stack trace to LogCat. If, afterwards, you are still unable to find the bug, return here and extend your question.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • @MeesterPatat: Even if you put some statement inside catch and put the breakpoint there? – Heinzi Sep 09 '13 at 09:28
  • Yes even after putting some lines of code and some breakpoints in the catch. – MeesterPatat Sep 09 '13 at 09:49
  • @MeesterPatat: I see. Strange. Like Iaalto, I do not completely trust breakpoints (especially when it comes to cross-thread issues), so my recommendation would be to add a few logging points and see if that give you any new insights... – Heinzi Sep 09 '13 at 10:22
0

Try this way

Handler mHandler = new Handler();

        mHandler.post(new Runnable() {

            @Override
            public void run() {
                 notifyObservers(fin);
            }
        });

instead of

activity.runOnUiThread(new Runnable() {
                            public void run() {
                             notifyObservers(fin);//I have breakpoint here, and this line is not executed.
                            }
                        });
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • 2
    This is essentially the same what `runOnUiThread()` does internally. Care to explain why this would solve the problem? – laalto Sep 09 '13 at 09:31