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);
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With