0

I had this issue: Why does my InputStream hold old values?

I thought I was able to solve it by the answer provided, but I haven't.

I am sending an IR signal to the IR receiver which is passed through the BT module to the InputStream of the Android application. Every time I press a button on the emitter, I send 100 bytes which then I expect to get into the InputStream.

Is there a way to ignore all incoming bytes after I get one packet for 3 seconds? Even if they arrive I want to skip them, simply don't put them into buffer. But after those 3 seconds I want again to listen to the incoming InputStream bytes flow.

I need that, because sometimes there are some dummy bytes coming from the IR emitter, or when user accidentally presses the button twice (I don't want the data to be sent twice). Simply I want some 3 seconds "cooldown".

My read function:

public int read(final InputStream input, final byte[] buffer) throws IOException {
            int remaining = buffer.length;
           while (remaining > 20) {
                final int location = buffer.length - remaining;
                final int count = input.read(buffer, location, remaining);
                if (count == -1) { // EOF
                    break;
                }
                remaining -= count;
            }
            return buffer.length - remaining;
        }

May I use a skip(long n) function? Can I define any number n, since I don't know how many bytes do I want to skip? I just want to skip all which came within those 3 seconds.

Community
  • 1
  • 1
Ondrej Tokar
  • 4,898
  • 8
  • 53
  • 103

1 Answers1

0

The skip method is a blocking one; once you call it, the next data flowing through the streamer will be lost and cannot be recovered, so you could lose data if nothing shows up before the bytes you are expecting. You could set a timer Thread sleeping for 3 seconds; when it awakes, you could perhaps make it change a static boolean value into the IR listener class, so when your read() method successfully retrieves data from the streamer you can decide whether to keep it or just ignore it.

StG
  • 257
  • 2
  • 11