BluetoothChat - sending issues.

Put your problem here if it does not fit any of the other categories.

BluetoothChat - sending issues.

Postby a_g » Thu Jul 08, 2010 7:47 pm

Hey, I've been trying to send an array of bytes from Android to ArduinoBT through Bluetooth. I'm using the SDK example "BluetoothChat" as base for my explorations - and so far I've managed to establish the connection and send data successfully. But, I'm having issues in transferring all data, the array is 160bytes long and I'm sending 16bytes at a time. For some reason the bytes are not being sent correctly, and I can only assume it's the way I am packing the bytes that's the issue.

Example: This array sends just perfectly, and the ArduinoBT confirms all 10 arrays of 16 bytes each.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         byte[] data = new byte[160];
  2.  
  3.                 for ( int i = 0; i < data.length; i += 3 ) {
  4.                         data[i] = (byte) 0x00;
  5.                 }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Example: This array does not sent perfectly, only 7 of the 10 arrays arrive "unharmed" to ArduinoBT. I've tested and tested again, it's always 7 so I sincerely doubt it's a connection issue.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         byte[] data = new byte[160];
  2.  
  3.                 byte[] data = new byte[160];
  4.  
  5.                 for ( int i = 0; i < data.length; i += 3 ) {
  6.                         data[i] = (byte) 0x0f;
  7.                 }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Here's the thread I'm using for sending the entire array...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /**
  2.          * This thread runs during a connection with a remote device. It handles all
  3.          * incoming and outgoing transmissions.
  4.          */
  5.         private class WriteThread extends Thread {
  6.                 private byte[] data;
  7.  
  8.                 public WriteThread( byte[] _data, BluetoothChatService _mChatService ) {
  9.                         data = _data;
  10.                 }
  11.  
  12.                 // This attaches header and the chksum to the byte array.
  13.                 private byte[] attachHeaderBytes( byte[] in, int length ) {
  14.                         byte[] outdata = new byte[in.length + 5];
  15.                         outdata[0] = (byte) 0xff; // Just pointing out the start...
  16.                         outdata[1] = (byte) 0x02; // The command we're sending to Arduino
  17.                         outdata[2] = (byte) 0x10; // We're using the size of 16...
  18.                         int index = 0;
  19.                         for ( int i = 3; i < outdata.length - 2; i++, index++ )
  20.                                 outdata[i] = in[index]; // Fill the data
  21.  
  22.                         outdata[outdata.length - 2] = (byte) ( outdata[1] ^ outdata[2] ); // checksum
  23.  
  24.                         for ( int i = 1; i > 0; i-- )
  25.                                 outdata[outdata.length - i] = (byte) 0xfe; // Garbage!
  26.  
  27.                         return outdata;
  28.                 }
  29.  
  30.                 public void run() {
  31.  
  32.                         int index = 0;
  33.                         int col = 0;
  34.                         int depth = 10;
  35.  
  36.                         // Keep listening to the InputStream while connected
  37.                         while ( true ) {
  38.                                 try {
  39.  
  40.                                         byte[] buffer = new byte[16];
  41.  
  42.                                         index = col;
  43.                                         // The data array is considered as 2D
  44.                                         for ( int i = 0; i < buffer.length; i++, index += depth ) {
  45.                                                 buffer[i] = data[index];
  46.                                         }
  47.  
  48.                                         // Attach the header and chksum bytes
  49.                                         buffer = attachHeaderBytes( buffer, buffer.length );
  50.  
  51.                                         mChatService.write( buffer );
  52.  
  53.                                         col++;
  54.  
  55.                                         if ( col == depth )
  56.                                                 break;
  57.  
  58.                                         // We only want to send every 250ms
  59.                                         Thread.sleep( 250 );
  60.                                 } catch ( InterruptedException e ) {
  61.                                         // TODO Auto-generated catch block
  62.                                         e.printStackTrace();
  63.                                 }
  64.                         }
  65.  
  66.                         Log.i( TAG, "Done, cancelling thread..." );
  67.                         return;
  68.                 }
  69.         }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4



Is there anyone who's really into serial communication who can inform me what I'm doing wrong, or point to a good resource for learning how to do serial communication over Bluetooth in a proper fashion. I'd really appreciate it.
a_g
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jul 08, 2010 6:37 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 21 guests