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 frommmInStream.read
? - Is there faster and better way to do this? Because as i understand copying one by one byte is slow?