Example: how to send big images over bluetooth (and also more text data)
===

I was looking for the solution of sending an object with some text data and image over bluetooth in my android app (without using bluetoothShare).

Now i have the solution based on BlueToothChat google example.

I have created an object with all data (texts+image converted to bytearray) i converted the bitmap on bytearray because as you know, you cannot serialize a bitmap directly...

I had a lot of reception problems so i have modified a little the bluetoothChatService...

the most important lines are on ConectedThread (run) and (write):





 

    public void run() {
                Log.i(TAG, "BEGIN mConnectedThread");
                byte[] buffer = new byte[1024*100];
                int bytes;
    
                // Keep listening to the InputStream while connected
              
    			int totalBytes = 0;
                
    			StringBuilder builder = new StringBuilder();
    			
                while (true) {
                    try {
                        // Read from the InputStream
                    	do {
    						bytes = mmInStream.read(buffer,0,1008);
    						String readMessage = new String(buffer, 0, bytes);
    						builder.append(readMessage);
    						totalBytes+=bytes;
    					} while (bytes==1008);
                        // Send the obtained bytes to the UI Activity
                        mHandler.obtainMessage(SendobjectActivity.MESSAGE_READ,builder.toString())
                                .sendToTarget();
                        builder.delete(0, builder.length());
                        
                    } catch (IOException e) {
                        Log.e(TAG, "disconnected", e);
                        connectionLost();
                        break;
                    }
                }
                if (totalBytes == 0 ){
         //esto solo lo hago para que no me aparezca el warning unused
                }
            }
    
            /**
             * Write to the connected OutStream.
             * @param buffer  The bytes to write
             */
            public void write(byte[] buffer) {
                try {
                    mmOutStream.write(buffer);
                    mmOutStream.flush();
    
                    // Share the sent message back to the UI Activity
                    mHandler.obtainMessage(SendobjectActivity.MESSAGE_WRITE, -1, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "Exception during write", e);
                }
            }



this is the little trick. i send the object converted into a String Base64...

if you want the full project example and working download freely from:

http://letitbit.net/download/19347.172fecf1b1790bfbd64b4d2115a6/sendobject.rar.html

maybe this can help to all people with problems sending over bluetooth with Android...






