Beno
Beno

Reputation: 739

Send long text message on android via Bluetooth

I am using the example app from android : BluetoothChat. But when I try to sent string that it size greater the 1024 byte the message don't transfer. I try to change the code below to send more then 1024 byte but I don't success in this. Please help me.

Read code:

public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(SmallWorld.MESSAGE_READ, bytes, -1,
                            buffer).sendToTarget();

                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }

send code:

public void write(byte[] buffer) {
        try {
            mmOutStream.write(buffer);

            // Share the sent message back to the UI Activity
            mHandler
                    .obtainMessage(SmallWorld.MESSAGE_WRITE, -1, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "Exception during write", e);
        }
    }

call to write:

    String message="blabla";
byte[] send = message.getBytes();
        mChatService.write(send);

Upvotes: 5

Views: 7457

Answers (1)

Dennis Mathews
Dennis Mathews

Reputation: 6975

After the write you may want to flush the stream to force the data to be sent out, because it could be that the stream is buffering the data and waiting for more before actuatlly sending out the data. Try ..

mmOutStream.write(buffer);
mmOutStream.flush();

Upvotes: 2

Related Questions