app/src/main/java/com/michael/bci/BciReceiver.java
public void ReadQueue(byte[] readData, int bytesAvailable, BciService bciService) {
        /* Ensure bytes available are not greater than TRANSFER_SIZE */
        if (bytesAvailable > 0)
        //Log.d(TAG, "bytesAvailable 2 " + bytesAvailable);
        {
            if (bytesAvailable > TRANSFER_SIZE) {
                bytesAvailable = TRANSFER_SIZE;
            }
            Log.d(TAG, "PROCESS_DATA 1a - queStatus: " + bytesAvailable);

            /*
            Read bytes from device.
            A call to read(byte[] data, int length) requesting up to available bytes will return with
            the data immediately (negative number for error)
            @param readData = Bytes array to store read bytes
            @param length = Amount of bytes to read
             */
            bciService.ftDevice.read(readData, bytesAvailable);
            Log.d(TAG, "readData_SIZE " + readData_SIZE);

            Log.w(TAG, "PROCESS_DATA 1d - Broadcast Intent:" + Arrays.toString(readData));
            if (bytesAvailable % readData_SIZE == 0 && readData[0] == START_BYTE) {
                Log.d(TAG, "received data " + bytesAvailable + " bytes " + ((float)bytesAvailable/ readData_SIZE));
            } else {
                Log.d(TAG, "received data " + bytesAvailable + " bytes ("+((float)bytesAvailable / readData_SIZE)+"), but readData size/start byte (" + readData[0] + ") is incorrect");
            }

            //Work we want completed
            try {
                Log.w(TAG, "PROCESS_DATA 1e - Broadcast Intent:" + Arrays.toString(readData));

                Channel channel = getChannel();
                Log.d(TAG, "RMQ: Connection/Channel 1 for EEG" +channel);


                if (readData[0] != START_BYTE) {
                    if (remainingBytes != null) {
                        byte[] dataTmp = new byte[remainingBytes.length + (readData.length - 1)];
                        System.arraycopy(remainingBytes, 0, dataTmp, 0, remainingBytes.length);
                        System.arraycopy(readData, 0, dataTmp, remainingBytes.length, readData.length -1);
                        remainingBytes = null;
                        readData = dataTmp;
                    }
                }

                /* Synchronise - Add a pre-loop to scan data stream based on START_BYTE locations */
                int iStart = 0;
                boolean found_start_byte = false;
                for (; iStart < readData.length -1; iStart++) {
                    if (readData[iStart] == START_BYTE && readData[iStart + 33] == START_BYTE && readData[iStart + 66] == START_BYTE) {
                        found_start_byte = true;
                        break;
                    }
                }

                if (iStart > (readData.length - 1))
                {
                    Log.w(TAG, "Error - we couldn't find a START_BYTE sync");
                    return; // Error - we couldn't find a sync
                }


                Log.w(TAG, "Start Byte" + found_start_byte );
                if (found_start_byte) {
                    for (int i = 0; i < readData.length - 1; i++) {
                        if (readData.length -1 > i + 32 && readData[i + 33] == START_BYTE && readData[i + 32] == END_BYTE) {
                            Log.w(TAG, "PROCESS_DATA 5:" + readData.length + "|" + i);


                            /*Encode a JSON object using LinkedHashMap so order of the entries is preserved
                            and moved JSON construction is in its own object */
                            LinkedHashMap<String, Serializable> obj = BciReceiver.jsonBciConstructor(readData, i);


                            i = i + 32;

                            //Log.w(TAG, "PROCESS_DATA 7: " + SampleNumber + ", " + ch1 + ", " + ch2 + "," + ch3 + ", " + ch4 + "," + ch5 + "," + ch6 + "," + ch7 +"," + ch8);
                            //Log.w(TAG, "PROCESS_DATA 8 - DATA: Loop" + i );

                            channel.basicPublish(EXCHANGE_NAME, "black", null, JSONValue.toJSONString(obj).getBytes(StandardCharsets.UTF_8));
                            Log.w(TAG, "PROCESS_DATA_JSON: Loop" + JSONValue.toJSONString(obj) );
                            Log.d(TAG, "USB: Bytes available to read from the Rx driver buffer: " + bciService.ftDevice.getQueueStatus()); //Retrieves the number of bytes available to read from the Rx driver buffer.
                            Log.d(TAG, "USB: buffer is full -> Rx pending until read: " + bciService.ftDevice.readBufferFull());
                        } else {
                            if (readData.length - 1 < TRANSFER_SIZE) {
                                if (readData.length -1 % readData_SIZE != 0) {
                                    remainingBytes = Arrays.copyOfRange(readData, i, (readData.length - 1));
                                    //Log.w(TAG, "PROCESS_DATA 9: " + Arrays.toString(remainingBytes));
                                    //Log.w(TAG, "PROCESS_DATA 10: " + Arrays.toString(remainingBytes));
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                // If we hit any issues, close the RabbitMQ channel, so that it is re-created on the next read.
                try {
                    //RabbitmqConnection.CloseConnection();
                    CloseChannel();
                    Log.d(TAG, "RMQ: Close Loop Channel for EEG");
                } catch (IOException | TimeoutException ioException) {
                    ioException.printStackTrace();
                }
            }

            //END LOOP HERE
        }
    }
/* ReadQueue Method reads data from the ftDevice device queue
     * Connects to the RabbitMQ broker and loops through the data */
ReadQueue Method reads data from the ftDevice device queue
Connects to the RabbitMQ broker and loops through the data
ReadQueue Method reads data from the ftDevice device queue Connects to the RabbitMQ broker and loops through the data
public void ReadQueue(byte[] readData, int bytesAvailable, BciService bciService) {
        /* Ensure bytes available are not greater than TRANSFER_SIZE */
        if (bytesAvailable > 0)
        {
            if (bytesAvailable > TRANSFER_SIZE) {
                bytesAvailable = TRANSFER_SIZE;
            }
            Log.d(TAG, "PROCESS_DATA 1a - queStatus: " + bytesAvailable);
            /*
            Read bytes from device.
            A call to read(byte[] data, int length) requesting up to available bytes will return with
            the data immediately (negative number for error)
            @param readData = Bytes array to store read bytes
            @param length = Amount of bytes to read
             */
            bciService.ftDevice.read(readData, bytesAvailable);
            Log.d(TAG, "readData_SIZE " + readData_SIZE);
            Log.w(TAG, "PROCESS_DATA 1d - Broadcast Intent:" + Arrays.toString(readData));
            if (bytesAvailable % readData_SIZE == 0 && readData[0] == START_BYTE) {
                Log.d(TAG, "received data " + bytesAvailable + " bytes " + ((float)bytesAvailable/ readData_SIZE));
            } else {
                Log.d(TAG, "received data " + bytesAvailable + " bytes ("+((float)bytesAvailable / readData_SIZE)+"), but readData size/start byte (" + readData[0] + ") is incorrect");
            }
            try {
                Log.w(TAG, "PROCESS_DATA 1e - Broadcast Intent:" + Arrays.toString(readData));
                Channel channel = getChannel();
                Log.d(TAG, "RMQ: Connection/Channel 1 for EEG" +channel);
                if (readData[0] != START_BYTE) {
                    if (remainingBytes != null) {
                        byte[] dataTmp = new byte[remainingBytes.length + (readData.length - 1)];
                        System.arraycopy(remainingBytes, 0, dataTmp, 0, remainingBytes.length);
                        System.arraycopy(readData, 0, dataTmp, remainingBytes.length, readData.length -1);
                        remainingBytes = null;
                        readData = dataTmp;
                    }
                }
                /* Synchronise - Add a pre-loop to scan data stream based on START_BYTE locations */
                int iStart = 0;
                boolean found_start_byte = false;
                for (; iStart < readData.length -1; iStart++) {
                    if (readData[iStart] == START_BYTE && readData[iStart + 33] == START_BYTE && readData[iStart + 66] == START_BYTE) {
                        found_start_byte = true;
                        break;
                    }
                }
                if (iStart > (readData.length - 1))
                {
                    Log.w(TAG, "Error - we couldn't find a START_BYTE sync");
                    return; 
                }
                Log.w(TAG, "Start Byte" + found_start_byte );
                if (found_start_byte) {
                    for (int i = 0; i < readData.length - 1; i++) {
                        if (readData.length -1 > i + 32 && readData[i + 33] == START_BYTE && readData[i + 32] == END_BYTE) {
                            Log.w(TAG, "PROCESS_DATA 5:" + readData.length + "|" + i);
                            /*Encode a JSON object using LinkedHashMap so order of the entries is preserved
                            and moved JSON construction is in its own object */
                            LinkedHashMap<String, Serializable> obj = BciReceiver.jsonBciConstructor(readData, i);
                            i = i + 32;
                            channel.basicPublish(EXCHANGE_NAME, "black", null, JSONValue.toJSONString(obj).getBytes(StandardCharsets.UTF_8));
                            Log.w(TAG, "PROCESS_DATA_JSON: Loop" + JSONValue.toJSONString(obj) );
                            Log.d(TAG, "USB: Bytes available to read from the Rx driver buffer: " + bciService.ftDevice.getQueueStatus()); 
                            Log.d(TAG, "USB: buffer is full -> Rx pending until read: " + bciService.ftDevice.readBufferFull());
                        } else {
                            if (readData.length - 1 < TRANSFER_SIZE) {
                                if (readData.length -1 % readData_SIZE != 0) {
                                    remainingBytes = Arrays.copyOfRange(readData, i, (readData.length - 1));
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    CloseChannel();
                    Log.d(TAG, "RMQ: Close Loop Channel for EEG");
                } catch (IOException | TimeoutException ioException) {
                    ioException.printStackTrace();
                }
            }
        }
    }
public void ReadQueue ( byte [ ] readData , int bytesAvailable , BciService bciService ) { /* Ensure bytes available are not greater than TRANSFER_SIZE */ if ( bytesAvailable > 0 ) { if ( bytesAvailable > TRANSFER_SIZE ) { bytesAvailable = TRANSFER_SIZE ; } Log . d ( TAG , "PROCESS_DATA 1a - queStatus: " + bytesAvailable ) ; /*
            Read bytes from device.
            A call to read(byte[] data, int length) requesting up to available bytes will return with
            the data immediately (negative number for error)
            @param readData = Bytes array to store read bytes
            @param length = Amount of bytes to read
             */ bciService . ftDevice . read ( readData , bytesAvailable ) ; Log . d ( TAG , "readData_SIZE " + readData_SIZE ) ; Log . w ( TAG , "PROCESS_DATA 1d - Broadcast Intent:" + Arrays . toString ( readData ) ) ; if ( bytesAvailable % readData_SIZE == 0 && readData [ 0 ] == START_BYTE ) { Log . d ( TAG , "received data " + bytesAvailable + " bytes " + ( ( float ) bytesAvailable / readData_SIZE ) ) ; } else { Log . d ( TAG , "received data " + bytesAvailable + " bytes (" + ( ( float ) bytesAvailable / readData_SIZE ) + "), but readData size/start byte (" + readData [ 0 ] + ") is incorrect" ) ; } try { Log . w ( TAG , "PROCESS_DATA 1e - Broadcast Intent:" + Arrays . toString ( readData ) ) ; Channel channel = getChannel ( ) ; Log . d ( TAG , "RMQ: Connection/Channel 1 for EEG" + channel ) ; if ( readData [ 0 ] != START_BYTE ) { if ( remainingBytes != null ) { byte [ ] dataTmp = new byte [ remainingBytes . length + ( readData . length - 1 ) ] ; System . arraycopy ( remainingBytes , 0 , dataTmp , 0 , remainingBytes . length ) ; System . arraycopy ( readData , 0 , dataTmp , remainingBytes . length , readData . length - 1 ) ; remainingBytes = null ; readData = dataTmp ; } } /* Synchronise - Add a pre-loop to scan data stream based on START_BYTE locations */ int iStart = 0 ; boolean found_start_byte = false ; for ( ; iStart < readData . length - 1 ; iStart ++ ) { if ( readData [ iStart ] == START_BYTE && readData [ iStart + 33 ] == START_BYTE && readData [ iStart + 66 ] == START_BYTE ) { found_start_byte = true ; break ; } } if ( iStart > ( readData . length - 1 ) ) { Log . w ( TAG , "Error - we couldn't find a START_BYTE sync" ) ; return ; } Log . w ( TAG , "Start Byte" + found_start_byte ) ; if ( found_start_byte ) { for ( int i = 0 ; i < readData . length - 1 ; i ++ ) { if ( readData . length - 1 > i + 32 && readData [ i + 33 ] == START_BYTE && readData [ i + 32 ] == END_BYTE ) { Log . w ( TAG , "PROCESS_DATA 5:" + readData . length + "|" + i ) ; /*Encode a JSON object using LinkedHashMap so order of the entries is preserved
                            and moved JSON construction is in its own object */ LinkedHashMap < String , Serializable > obj = BciReceiver . jsonBciConstructor ( readData , i ) ; i = i + 32 ; channel . basicPublish ( EXCHANGE_NAME , "black" , null , JSONValue . toJSONString ( obj ) . getBytes ( StandardCharsets . UTF_8 ) ) ; Log . w ( TAG , "PROCESS_DATA_JSON: Loop" + JSONValue . toJSONString ( obj ) ) ; Log . d ( TAG , "USB: Bytes available to read from the Rx driver buffer: " + bciService . ftDevice . getQueueStatus ( ) ) ; Log . d ( TAG , "USB: buffer is full -> Rx pending until read: " + bciService . ftDevice . readBufferFull ( ) ) ; } else { if ( readData . length - 1 < TRANSFER_SIZE ) { if ( readData . length - 1 % readData_SIZE != 0 ) { remainingBytes = Arrays . copyOfRange ( readData , i , ( readData . length - 1 ) ) ; } } } } } } catch ( Exception e ) { e . printStackTrace ( ) ; try { CloseChannel ( ) ; Log . d ( TAG , "RMQ: Close Loop Channel for EEG" ) ; } catch ( IOException | TimeoutException ioException ) { ioException . printStackTrace ( ) ; } } } }
ReadQueue Method reads data from the ftDevice device queue
 Connects to the RabbitMQ broker and loops through the data
ReadQueue Method reads data from the ftDevice device queue Connects to the RabbitMQ broker and loops through the data
//Log.d(TAG, "bytesAvailable 2 " + bytesAvailable); //Work we want completed // Error - we couldn't find a sync //Log.w(TAG, "PROCESS_DATA 7: " + SampleNumber + ", " + ch1 + ", " + ch2 + "," + ch3 + ", " + ch4 + "," + ch5 + "," + ch6 + "," + ch7 +"," + ch8); //Log.w(TAG, "PROCESS_DATA 8 - DATA: Loop" + i ); //Retrieves the number of bytes available to read from the Rx driver buffer. //Log.w(TAG, "PROCESS_DATA 9: " + Arrays.toString(remainingBytes)); //Log.w(TAG, "PROCESS_DATA 10: " + Arrays.toString(remainingBytes)); // If we hit any issues, close the RabbitMQ channel, so that it is re-created on the next read. //RabbitmqConnection.CloseConnection(); //END LOOP HERE