Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 795
Beno Avatar asked Nov 05 '22 18:11

Beno


1 Answers

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();
like image 140
Dennis Mathews Avatar answered Nov 15 '22 06:11

Dennis Mathews