2

I have a app that connects by bluetooth with Arduino. The app is ok, but when I start an another activity my textview doesn't update. I have a Thread that reads the data of bluetooth, and I have a timer that refreshes the textview.

If you start activity the first time and I return the main activity the textview refreshes ok, but if I start the activity again when I return the main the textview doesn't refresh.

HELP!!!

OnCreate:

bluetoothIn = new Handler() {
    public void handleMessage(android.os.Message msg) {
        if (msg.what == handlerState) {
            String readMessage = (String) msg.obj;
            MetrosRecorridos += ((Calibracion / Imanes / 1000) * readMessage.length()) * Sentido;
        }
    }
};

in button that connects with bluetooth:

mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();

The Thread:

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

    //creation of the connect thread
    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        try {
            //Create I/O streams for connection
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[256];
        int bytes;

        // Keep looping to listen for received messages
        while (true) {
            try {
                bytes = mmInStream.read(buffer);            //read bytes from input buffer
                String readMessage = new String(buffer, 0, bytes);
                // Send the obtained bytes to the UI Activity via handler
                bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
            } catch (IOException e) {
                txtConectado = "Sonda: Desconectado";
                //Toast.makeText(getBaseContext(), "Fallo de conexión", Toast.LENGTH_LONG).show();
                break;
            }
        }
    }
    //write method
    public void write(String input) {
        byte[] msgBuffer = input.getBytes();           //converts entered String into bytes
        try {
            mmOutStream.write(msgBuffer);                //write bytes over BT connection via outstream
        } catch (IOException e) {
            //if you cannot write, close the application
            Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
            finish();

        }
    }
}

The Timer that refreshes the textview:

public void startTimer(){
    t = new Timer();
    task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView t;
                    t=(TextView)findViewById(R.id.txtA);
                    t.setText(""+MetrosRecorridos);
                }
            });
        }
    };
    t.scheduleAtFixedRate(task, 0, 10);
}

And the code when I call another activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.opc_ajustes) {
        bluetoothIn.removeCallbacksAndMessages(null);
        Intent i = new Intent(getApplicationContext(), AjustesActivity.class);
        i.putExtra("distancia", Math.floor(MetrosRecorridos / 10));
        startActivityForResult(i, 3);
        return true;
    }
}

Thanks!!!

Ben Hocking
  • 7,790
  • 5
  • 37
  • 52

1 Answers1

1

I can't understand the problem clearly. But i think thread stops when activity on pause. In that case you should create a singleton timer for that purpose or use timer into application class. In activity inside the onResume method , start another thread for refreshing Textview. I hope this helps

Global Timer in android

Community
  • 1
  • 1
Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26
  • Sorry, i from spanish and my english is very bad. But, because the first time it does well and when I run the activity for the second time for the thread? – r_ameijeiras Jul 09 '15 at 22:36
  • It might occurs because of multiple reasons such as memory leak or you tries to recreate the same thing again or another thread might blocks it etc.. Thats why you should use singleton method. In the worst scenario if it is not working , you can save timer values into the internal and read it again on resume and recall the thread again. – Abdullah Tellioglu Jul 09 '15 at 23:05