Java 如何在USB通信中逐字节读取?

Java 如何在USB通信中逐字节读取?,java,android,bluetooth,usb,Java,Android,Bluetooth,Usb,我想使用USB连接,如以下代码中的蓝牙连接: public void run() { byte[] buffer; // buffer store for the stream 1024 int bytes=0; // bytes returned from read() while (true){ buffer = new byte[1]; try { for (i

我想使用USB连接,如以下代码中的蓝牙连接:

public void run() {
        byte[] buffer;  // buffer store for the stream 1024
        int bytes=0; // bytes returned from read()

        while (true){
            buffer = new byte[1];


            try {

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

                bytes = mmInStream.read(buffer);

                result[i] = buffer[0];

            }

            mHandler.obtainMessage(MainActivity.READ, bytes, -1, result)
                    .sendToTarget();
            mHandler.sendEmptyMessage(0);

                while (mmInStream.available()>0){
                    mmInStream.read(buffer);
                }

            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
        }

    }
你能帮我写一个USB读取的代码吗?它在蓝牙通信中像队列或管道一样工作

编辑

我尝试了另一个代码:

while (true){
            readBytes = new byte[1]; 


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

                recvBytes = connection.bulkTransfer(epIn, readBytes, readBytes.length, 100);

                Log.e("bbb", "!" + (new String(readBytes)) + "!" + recvBytes + " " + i);

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }


        }

不明白你所说的排队或管道是什么意思。但是如果你想一次读一个bye,那么就把
,readBytes.length,
改为
,1,
,bytes=mmInStream.read(缓冲区)。如果您想一次读取一个字节,则不需要缓冲区
int byte=mmInStream.read()我已经编辑了我的问题。我指的是队列或管道,如mmInStream.read()的情况;因为它只读取接收到的字节,然后停止/等待,但在usb通信的情况下,它会与日志永久循环,正如您在我问题的编辑部分中所看到的。
因为它只读取接收到的字节。
?你的意思是:‘它只是读取发送的字节’?是的,对不起,我的英语不是很好。
while (true){
            readBytes = new byte[1]; 


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

                recvBytes = connection.bulkTransfer(epIn, readBytes, readBytes.length, 100);

                Log.e("bbb", "!" + (new String(readBytes)) + "!" + recvBytes + " " + i);

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }


        }
02-10 13:24:42.210 14602-14755/? E/bbb: !����������������������������!-1 0
02-10 13:24:42.261 14602-14755/? E/bbb: !����������������������������!-1 1
02-10 13:24:42.312 14602-14755/? E/bbb: !����������������������������!-1 2
02-10 13:24:42.362 14602-14755/? E/bbb: !����������������������������!-1 3
02-10 13:24:42.412 14602-14755/? E/bbb: !����������������������������!-1 4
02-10 13:24:42.463 14602-14755/? E/bbb: !����������������������������!-1 5
02-10 13:24:42.513 14602-14755/? E/bbb: !����������������������������!-1 6
02-10 13:24:42.564 14602-14755/? E/bbb: !����������������������������!-1 7
02-10 13:24:42.614 14602-14755/? E/bbb: !����������������������������!-1 8
02-10 13:24:42.665 14602-14755/? E/bbb: !����������������������������!-1 9
02-10 13:24:42.716 14602-14755/? E/bbb: !����������������������������!-1 10
02-10 13:24:42.766 14602-14755/? E/bbb: !����������������������������!-1 11
02-10 13:24:42.817 14602-14755/? E/bbb: !����������������������������!-1 12
02-10 13:24:42.867 14602-14755/? E/bbb: !����������������������������!-1 13