简体   繁体   English

为什么我的InputStream保留旧值?

[英]Why does my InputStream hold old values?

I am sending an IR signal to the IR receiver which is passed through the BT module to the InputStream of the Android application. 我正在向IR接收器发送IR信号,该信号通过BT模块传递到Android应用程序的InputStream 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 handle an exception when there is no 100 bytes because the packet got corrupted?). 每次我按下发射器上的一个按钮时,我都会发送100 bytes ,然后我希望它进入InputStream (是否存在一种方法来处理由于包损坏而没有100字节的异常?)。

This is my code for reading InputStream and putting values into the byte[] buffer : 这是我的代码,用于读取InputStream并将值放入byte[] buffer

 public int read(final InputStream input, final byte[] buffer) throws IOException {
        int remaining = buffer.length;
        try {
            Thread.sleep(100); // Not sure if this helps anything, just a desperate move
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (remaining > 0) {
            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;
    }

It works fine for couple of times, but at some point, usually after 4th try, I am getting those mentioned bytes without the first byte. 它可以正常工作几次,但是在某些时候,通常是经过4次尝试后,我得到的那些提到的字节没有第一个字节。 It seems like package has been corrupted at some point and only 99 bytes were sent and then subsequently the next sending of the bytes, result in putting one byte to the previous buffer and losing the first byte. 似乎程序包在某个时刻已损坏,仅发送了99个字节,随后又发送了下一个字节,结果是将一个字节放入上一个缓冲区,并丢失了第一个字节。

However interesting is, that packets have always 100 bytes, and they are not dummy (zero values) in there. 但是有趣的是,数据包始终具有100个字节,并且在那里不是虚拟的(零值)。

Those values are accelerometer coordinates (doesn't really matter). 这些值是加速度计坐标(并不重要)。

Example correct values: 正确值示例:

0 = 1
1 = 35
2 = 0
3 = -27
4 = 19
5 = -4
6 = 64
7 = 10
8 = -7
9 = 66
10 = 10
11 = 0
12 = 66
13 = 4
14 = -1
15 = 64
16 = 8
17 = -1
18 = 67
19 = 7
20 = -3
21 = 66
22 = 6
23 = -1
24 = 65
25 = 7
26 = -2
27 = 66
28 = 7
29 = -3
30 = 65
31 = 6
32 = -3
33 = 67
34 = 6
35 = -2
36 = 66
37 = 7
38 = -2
39 = 66
40 = 4
41 = -3
42 = 66
43 = 6
44 = -3
45 = 66
46 = 6
47 = -3
48 = 66
49 = 5
50 = -3
51 = 66
52 = 6
53 = -2
54 = 65
55 = 5
56 = -3
57 = 65
58 = 6
59 = -3
60 = 66
61 = 6
62 = -3
63 = 66
64 = 6
65 = -3
66 = 66
67 = 6
68 = -2
69 = 66
70 = 5
71 = -3
72 = 66
73 = 5
74 = -3
75 = 66
76 = 5
77 = -2
78 = 66
79 = 5
80 = -3
81 = 66
82 = 5
83 = -3
84 = 66
85 = 6
86 = -2
87 = 66
88 = 5
89 = -2
90 = 65
91 = 5
92 = -2
93 = 65
94 = 5
95 = -2
96 = 66
97 = 4
98 = -2
99 = 66

Example incorrect values (it can also happen that first byte is missing, however in this case there is one more byte at the beginning): 错误值的示例(丢失第一个字节也可能发生,但是在这种情况下,开头还有一个字节):

0 = 66
1 = 1
2 = 35
3 = 3
4 = 10
5 = -14
6 = -17
7 = 81
8 = 15
9 = -5
10 = 64
11 = 14
12 = -4
13 = 68
14 = 7
15 = -5
16 = 69
17 = 9
18 = -5
19 = 78
20 = 15
21 = -3
22 = 77
23 = 16
24 = -2
25 = 72
26 = 20
27 = 3
28 = 64
29 = 20
30 = 8
31 = 55
32 = 22
33 = 16
34 = 50
35 = 22
36 = 19
37 = 52
38 = 11
39 = 13
40 = 50
41 = 6
42 = 14
43 = 50
44 = 9
45 = 13
46 = 54
47 = 12
48 = 9
49 = 63
50 = 16
51 = 12
52 = 67
53 = 6
54 = 0
55 = 74
56 = 8
57 = -4
58 = 75
59 = 13
60 = -2
61 = 68
62 = 17
63 = -5
64 = 79
65 = 8
66 = -8
67 = 62
68 = 15
69 = -8
70 = 65
71 = 13
72 = -7
73 = 67
74 = 8
75 = -6
76 = 66
77 = 9
78 = -3
79 = 67
80 = 8
81 = -4
82 = 66
83 = 7
84 = -4
85 = 65
86 = 8
87 = -4
88 = 67
89 = 7
90 = -4
91 = 66
92 = 7
93 = -3
94 = 67
95 = 6
96 = -4
97 = 66
98 = 6
99 = -3

Any idea how to solve it? 知道如何解决吗? Note, I don't send 2 packages of bytes twice in less than 3 seconds, if that helps in finding a solution. 请注意,如果能帮助您找到解决方案,我不会在不到3秒的时间内两次发送2个字节的数据包。

InputStream is a very low level API, you almost never want to use it directly. InputStream是一个非常低级的API,您几乎永远不想直接使用它。 In this case, the wrapper that is most suited to your purposes is DataInputStream , which has a readFully method that seems to be what you want; 在这种情况下,最适合您目的的包装器是DataInputStream ,它具有一个readFully方法,该方法似乎正是您想要的。 the code is much simpler and shorter. 代码更简单,更短。

public void read(final InputStream input, final byte[] buffer) throws IOException, EOFException {
  DataInputStream dataStream = new DataInputStream(input);
  dataStream.readFully(buffer, 0, buffer.length);
}

If this doesn't work, then I suspect the problem is something with the data stream from the IR itself. 如果这不起作用,那么我怀疑问题出在IR本身的数据流中。


If you need to read from the stream with a timeout (in the case of a corrupted packet), then this method is not advised, because it will block, as you've already noticed. 如果您需要在超时的情况下从流中读取数据(在数据包损坏的情况下),则不建议使用此方法,因为它会阻塞,正如您已经注意到的那样。 In such a case, I recommend reading the information in this question: Is it possible to read from a InputStream with a timeout? 在这种情况下,我建议阅读以下问题中的信息: 是否可以在超时的情况下从InputStream读取?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM