0

I have an issue that seems to persist about 70% of the time - my USB Thread's handler is not called in a looper.

In run() I have

//Sends messages to usb accessory
Looper.prepare();
controlSender = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        try{
            Log.d("USB Thread", "Writing out to USB filestream");
            output.write((byte[])msg.obj);
        }catch(final Exception e){
            Log.d("USB Thread", "Writing to USB filestream failed");
            e.printStackTrace();
            UIHandler.post(new Runnable() {
                @Override
                public void run() {
                    onNotify("USB Send Failed " + e.toString() + "\n");
                }
            });
        }
    }
};
Looper.loop();

Neither Log is ever called when this problem happens, and messages are being received on this thread as well (might be an issue?). this.start(); is called whenever the accessory is connected, and I call constrolSender.getLooper().quit(); whenever it's disconnected.

JuJoDi
  • 14,627
  • 23
  • 80
  • 126

1 Answers1

2

A thread can only be started once in Java and there is no time guarantee on when it will actually be started. If you try to post a message to it before the thread starts the the Looper starts looping then the message will be dropped. Try starting your thread well before you need to post message to make sure it is started on time.

Gabe
  • 1,239
  • 1
  • 13
  • 20
  • By "can only be started once" do you mean that I cannot call this.start() more than once to the same affect as the first time? – JuJoDi Dec 26 '13 at 21:14
  • correct. More info: http://stackoverflow.com/questions/1215548/is-it-legal-to-call-the-start-method-twice-on-the-same-thread – Gabe Dec 26 '13 at 21:20