views:

30

answers:

0

Hey everyone Using the SDK example BluetoothChat I've been experimenting sending byte[] to ArduinoBT in smaller chunks. For example, I'm sending a byte[] of size 800. I can perfectly send this array, but only under specific circumstances. The part that is puzzling me at the moment is why some arrays are not being sent correctly where others are.

Example: These arrays are being sent and registered by ArduinoBT properly.

byte[] arr = new [160];
for( int i = 0; i < arr.length; i++ )
    arr[i] = (byte)0;

byte[] arr = new [160];
for( int i = 0; i < arr.length; i+=2 )
    arr[i] = (byte)0;

byte[] arr = new [160];
for( int i = 0; i < arr.length; i+=4 )
    arr[i] = (byte)0;

However, the following arrays are not being sent properly - even if I first initialize every index in the array to "(byte)0".

byte[] arr = new [160];
for( int i = 0; i < arr.length; i+=3 )
    arr[i] = (byte)0;

byte[] arr = new [160];
for( int i = 0; i < arr.length; i+=5 )
    arr[i] = (byte)0;

byte[] arr = new [160];
for( int i = 0; i < arr.length; i+=7 )
    arr[i] = (byte)0;

I'm sending the arrays in a very simple thread and attaching header/footer information which is then used by ArduinoBT to verify the package.

private class WriteThread extends Thread {
    private byte[] data;

    public WriteThread( byte[] _data, BluetoothChatService _mChatService ) {
        data = _data.clone();
        Log.i( TAG, "here's the data length... " + data.length );
    }

    // This attaches header and the chksum to the byte array.
    private byte[] attachHeaderBytes( byte[] in ) {
        byte[] header = { (byte) 0xff, 0x02, 0x10 };

        byte[] footer = { (byte) ( header[1] ^ header[2] ) };

        // Add them all together
        byte[] outdata = new byte[header.length + in.length + footer.length];
        int i = 0;
        for ( int index = 0; index < header.length; index++, i++ )
            outdata[i] = header[index];
        for ( int index = 0; index < in.length; index++, i++ )
            outdata[i] = in[index];
        for ( int index = 0; index < footer.length; index++, i++ )
            outdata[i] = footer[index];

        return outdata;
    }

    public void run() {

        int index = 0;

        // Keep listening to the InputStream while connected
        while ( true ) {
            try {

            byte[] buffer = new byte[16];

            Log.i( TAG, "index = " + index );

            for ( int i = 0; i < buffer.length; i++, index++ )
                buffer[i] = (byte) ( data[index] & 0xFF );

            // Attach the header and chksum bytes
            buffer = attachHeaderBytes( buffer );

            String s = toHex( buffer );
            Log.i( TAG, "sending(hex): " + s );

            mChatService.write( buffer );

            if ( index == data.length )
                break;

            // We only want to send every 250ms
            Thread.sleep( 250 );
            } catch ( InterruptedException e ) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        Log.i( TAG, "Done, cancelling thread..." );
        return;
    }
}

I'm not very good at serial communication, so please inform me if I'm doing something completely weird here. Also, I've been wondering if the unsigned/signed might have anything to do with my issues - Arduino uses unsigned bytes by default.

Thanks in advance.