0

I am sending 90000 bytes data from Bluetooth.I am getting chunks of 1024 bytes at receiving side. I need to collected them in single array and make an byte array. Not able to do that..

This is the code:

    private final Handler mHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        System.out.println("***************handleMessage***************"+msg.obj);
        switch (msg.what) {
        case MESSAGE_STATE_CHANGE:
            if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
            switch (msg.arg1) {
            case BluetoothChatService.STATE_CONNECTED:
                mTitle.setText(R.string.title_connected_to);
                mTitle.append(mConnectedDeviceName);
                mConversationArrayAdapter.clear();
                break;
            case BluetoothChatService.STATE_CONNECTING:
                mTitle.setText(R.string.title_connecting);
                break;
            case BluetoothChatService.STATE_LISTEN:
            case BluetoothChatService.STATE_NONE:
                mTitle.setText(R.string.title_not_connected);
                break;
            }
            break;
        case MESSAGE_WRITE:
            byte[] writeBuf = (byte[]) msg.obj;
            // construct a string from the buffer
            String writeMessage = new String(writeBuf);
            mConversationArrayAdapter.add("Me:  " + writeMessage);
            break;
        case MESSAGE_READ:
            System.out.println("***************msg.obj***************"+msg.obj);
            byte[] readBuf = (byte[]) msg.obj;

            System.out.println("***************readBuf***************"+readBuf.length);
            Bitmap bmp=BitmapFactory.decodeByteArray(readBuf,0,readBuf.length);
            System.out.println("***************bmp***************"+bmp);

            //convert it back to an image
            ByteArrayInputStream imageStream = new ByteArrayInputStream(readBuf);
            Bitmap theImage = BitmapFactory.decodeStream(imageStream);
            System.out.println("***************theImage***************"+theImage);
            try
            {
            String filepath=Environment.getExternalStorageDirectory().getAbsolutePath();
            File file = new File(filepath, "barcode.PNG");
            FileOutputStream fos = new FileOutputStream(file); 
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            theImage.compress(CompressFormat.JPEG, 100, fos);
            bos.flush();
            bos.close();
            }
            catch(Exception e)
            {
                System.out.println("***************Read***************"+e);
            }

case MESSAGE_READ: is calling again and again. I am sending image through bluetooth. at the sending side and getting 1024 bytes chunks at receiving side..

Any help would be appreciated.

Thanks

3
  • you'll need to be a little more specific. Receiving them with what? What is your server doing now? What language is it built in? etc. Commented Nov 28, 2011 at 8:21
  • And show the code where you receive your chunks. So we can see what you have so far.. Commented Nov 28, 2011 at 8:23
  • hey Android_D howz you? i am also doing the same thing as u did (means i am also trying to send image to second paired device but whole bytes are not receiving to other end) and facing same problem as your, have you found any solution of this..? please suggest if you have any solution ...thanks Commented Mar 16, 2012 at 5:14

1 Answer 1

3

Write them to a ByteArrayOutputStream, pre-allocate if you know the whole size beforehand. Call toArray() when done to get the result.

Sign up to request clarification or add additional context in comments.

5 Comments

@Android_D This is the way to go. Don't create the Bitmap until you have all the data buffered.
@Mister Smith: how can i do that
Once you have received all the data and writed it to the ByteArrayOutputStream, call ByteArrayOutputStream.toArray() to get the full byte array, then pass to your image creation code as usual.
@Mister Smith: Problem is this i am not able to collect all the data.
Declare a ByteArrayOutputStream variable outside the method, then as the method is called "again and again", in each call you write a chunk, and finally (by some mean) detect that a given chunk is the final one and create the image.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.