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);