Android Open Source - simple-android-bt Read Write Thread






From Project

Back to project page simple-android-bt.

License

The source code is released under:

MIT License

If you think the Android project simple-android-bt listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package io.generalpurpose.sandbox;
//ww w .  ja v  a2 s.  c o  m
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by nathan on 4/3/14.
 */
public class ReadWriteThread extends Thread {
    private static final String TAG = "ReadWriteThread";
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    UpdateReceivedRunnable updateReceivedRunnable;
    Handler mHandler;

    public ReadWriteThread(BluetoothSocket socket, UpdateReceivedRunnable _updateReceivedRunnable) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        updateReceivedRunnable = _updateReceivedRunnable;

//        mHandler = new Handler(Looper.getMainLooper());

        // Get the input and output streams, using temp objects because
        // member streams are final
        try {
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[1024];  // buffer store for the stream
        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);
                // Send the obtained bytes to the UI activity
//                mHandler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1, buffer)
//                        .sendToTarget();

                Log.d(TAG, String.format(
                        "number of received bytes = %d",
                        bytes
                ));
                updateReceivedRunnable.run(bytes, buffer);
            } catch (IOException e) {
                break;
            }
        }
    }

    /* Call this from the main activity to send data to the remote device */
    public void write(byte[] bytes) {
        try {
            mmOutStream.write(bytes);
        } catch (IOException e) { }
    }

    /* Call this from the main activity to shutdown the connection */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) { }
    }
}




Java Source Code List

io.generalpurpose.sandbox.ConnectThread.java
io.generalpurpose.sandbox.MainActivity.java
io.generalpurpose.sandbox.ManageSocketRunnable.java
io.generalpurpose.sandbox.ReadWriteThread.java
io.generalpurpose.sandbox.UpdateReceivedRunnable.java