Android & Bluetooth & Arduino

I am trying to display sensor data on my Android phone (target 4.3) received from a transmitting arduino type device. The transmission takes place via Bluetooth. I am able to connect to the arduino type device and even share data, however for some reason I am having synchronization issues.

The way the arduino is setup right now, after a successful connection it waits for a byte to be received from my phone (unsigned byte value 255), when it receives this byte it responds by sending a packet (3 bytes) containing information from three different sensors i.e.

byte 1: temperature data
byte 2: cadence data
byte 3: speed data

All I have to do is display this data(which is updating live) repeatedly until the user terminates the connection on the android phone.

Here is my code I feel I am making a minor error somewhere in my logic.


Handler mHandler = new Handler(){           
            public void handleMessage(Message msg){
                    case SUCCESS_CONNECT:
                        // Do Something;
                        ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
                         * Could send test string here
                         * String connect_string = "test";
                         * connectedThread.write(connect_string.getBytes());
                    case MESSAGE_READ:
                        byte[] readBuf = (byte[])msg.obj;
                        int tempInt = byteToInt(readBuf[0]);
                        int cadenceInt = byteToInt(readBuf[1]);
                        int speedInt = byteToInt(readBuf[2]);
                        EditText temperatureData = (EditText)getActivity().findViewById(;
                        EditText cadenceData = (EditText)getActivity().findViewById(;
                        EditText speedData = (EditText)getActivity().findViewById(;



public class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    public ConnectThread(BluetoothDevice device) {

         *  Use a temporary object that is later assigned to mmSocket,
         *  because mmSocket is final                

        BluetoothSocket tmp = null;

        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) { }
        mmSocket = tmp;

    public void run() {
        // Cancel discovery because it will slow down the connection

        try {
        // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
        } catch (IOException connectException) {
            // Unable to connect; close the socket and get out
            try {
            } catch (IOException closeException) {
                Toast.makeText(getActivity(), "Connecting to device failed!", Toast.LENGTH_LONG).show();

            // Do work to manage the connection (in a separate thread)
            mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
           } catch (IOException e) { }



private class ConnectedThread extends Thread {
    private final BluetoothSocket mmSocket;
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    public ConnectedThread(BluetoothSocket socket) {
        mmSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        // 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; // 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
                buffer = new byte[3];
                byte maxByte = (byte) 1;
                bytes =,0,buffer.length);
                // Send the obtained bytes to the message handler

             catch (IOException e) {

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

Byte To Int Method

public static int byteToInt(byte b){
    int value;
    value = b & 0xFF;
    return  value;

The data I receive is being displayed but often ends up wrong mostly because the byte array sequence is off which causes the wrong values to be displayed. I have been trying to figure this out for a while and any input would be helpful.


Check if you can send in the message to the handler a cloned array. It should be something like "buffer.clone" or "buffer.clone()" instead of simply "buffer". If it is so, it means that the not-cloned buffer is copied as reference to the handler. While the handler is doing its stuff, the connected thread may redefine the array and reassign to it different values. To test this you can also define the buffer ad Byte[] instead of byte[]. In this way I fixed similar problem in my application.

Need Your Help

Regex to capture an exact word in a sentence

c# regex

I'm having some trouble to capture a specific string inside of a sentence.

Python showing error - name 'Object' is not defined


Python interpreter is showing NameError on using Object.