3

I have modified the Android Bluetooth Chat sample application to now send images across. This is fine for the first image. It gets sent across and displays correctly. When I try to send another image, it seems to send the previous image across 20+ times, when it should just send the new image over once. I have tried using oef's but to no avail.

This sends the picture:

        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.rc_a);

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        icon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        byte[] image = bytes.toByteArray();

        mConnection.write(image);

This is in the ConnectedThread:

    public void run() {
        byte[] buffer = new byte[1024];
        byte[] imgBuffer = new byte[1024 * 1024];
        int pos = 0;

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                int bytes = mmInStream.read(buffer);
                System.arraycopy(buffer,0,imgBuffer,pos,bytes);
                pos += bytes;

                mHandler.obtainMessage(BtoothSetupActivity.MESSAGE_READ,
                        pos, -1, imgBuffer).sendToTarget();

            } catch (IOException e) {
                connectionLost();
                break;
            }
        }
    }

This reads the data back in:

    case MESSAGE_READ:
    byte[] readBuf = (byte[]) msg.obj;
    Bitmap bmp = BitmapFactory.decodeByteArray(readBuf, 0, msg.arg1);
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

2

For transferring files you can make an explicit call to ACTION_SEND using intents.

With ACTION_SEND, a menu will popup with the application that can handle the file type you want to send, from which the user will need to select Bluetooth, and then the device of which to send the file.

File sourceFile = new File("//mnt/sdcard/file.apk"); 
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
Intent.setType("image/jpeg"); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sourceFile));
startActivity(intent);

Additional Help:

Community
  • 1
  • 1
syb0rg
  • 8,057
  • 9
  • 41
  • 81
  • The idea is to control another devices camera (which is implemented), then send the captured image across. So ideally there is no human interaction with the camera device (only to set up the application). –  Mar 15 '13 at 19:26
  • See the links provided for more help on your issue. – syb0rg Mar 15 '13 at 19:53