2

I am developing an app for my own use. Android device communicates with other devices. Earlier i was communicating via Bluetooth, but at the moment i am changing to USB communication. I use ksksue FTDriver. And i ran into thinking loop and can't break from it :)

Basically, there are fixed 6 bytes data i need to receive from device. 2 data bytes and 4 CRC bytes. I was taking 1 byte by 1 and adding it to other byte array, when reach 6 bytes then recopy them to new byte arrays and deal with them if CRC equal.

This is from Bluetooth app

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

            // Keep listening to the InputStream until an exception occurs
            while (true) {
                try {
                    bytesAvailable = mmInStream.read(buffer);

                    System.arraycopy(buffer, 0, input_bytes_from, last, buffer.length);
                    last++;
                    if (last == 6){
                        last = 0;
                        Log.d(TAG,"received "+bytesToHex(input_bytes_from));
                    byte[] data_bytes = new byte[2];
                    byte[] crc_to_compare = new byte[4];

                    for(byte i=0;i<data_bytes.length;i++)data_bytes[i] = input_bytes_from[i];

                    for(int i=0;i<4;i++){
                        crc_to_compare[i] = input_bytes_from[i+data_bytes.length]; 
                       }
                    crc_test(data_bytes,crc_to_compare);

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

        }
  • What happens to byte/s when bytesAvailable = mmInStream.read(buffer); is called? Is one byte copied and deleted from mmInStream.read?
  • Is there faster and better way to do this? Because as i understand copying one by one byte is slow?
Martynas
  • 627
  • 2
  • 8
  • 28

1 Answers1

0
  • What happens to byte/s when bytesAvailable = mmInStream.read(buffer); is called? Is one byte copied and deleted from mmInStream.read?

Yes. One byte is removed from the InputStream, and the byte is copied into the buffer.

  • Is there faster and better way to do this? Because as i understand copying one by one byte is slow?

You don't need to read bytes one by one. Refer to the following code.

public void run() {
    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            byte[] data_bytes = new byte[2];
            byte[] crc_to_compare = new byte[4];

            mmInStream.read(data_bytes);
            mmInStream.read(crc_to_compare);

            crc_test(data_bytes, crc_to_compare);
        } catch (IOException e) {
            break;
        }
    }
}

There is an open source framework for serial communication with other devices, called NFCommunicator.

https://github.com/Neofect/NFCommunicator

It could help you make an Android application which talks to other devices via serial protocol like Bluetooth SPP, though it is not for USB connection at the moment.

neokim
  • 80
  • 10