我已经修改了 Android 蓝牙聊天示例应用程序,现在可以发送图像。这对于第一张图像来说很好。它被发送并正确显示。当我尝试发送另一个图像时,它似乎发送了 20 多次以前的图像,而它应该只发送一次新图像。我尝试过使用oef,但无济于事。
这发送图片:
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);
这是在 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;
}
}
}
这会读回数据:
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
Bitmap bmp = BitmapFactory.decodeByteArray(readBuf, 0, msg.arg1);