how to perform all tasks at once?

Asked

Viewed 196 times

1

I can’t get you to perform all these tasks at the same time, only perform the rest when you get some data on InputStream. And once he gets that data, he doesn’t get any more. Please help me, I’m new and I’ve tried several examples but I can’t make it work.

package com.vaiporra;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.SystemClock;

import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;

import android.widget.Toast;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.io.IOException;



public class MainActivity extends Activity{

    Button btDireita; 
    Button btEsquerda;
    Chronometer crono1;
    char serial;
    BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
    private BluetoothDevice dispositivoBluetoohRemoto;
    private BluetoothSocket meuSocket;
    private OutputStream outputStream=null;
    private InputStream inputStream = null;
    private static final String MAC = "60:BE:B5:7A:FD:49";
    private static final UUID MEU_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (!bluetooth.isEnabled()) {
            bluetooth.enable();
        } 

        conecta();

        ChamaPrincipal();
    }

    private void ChamaPrincipal() {
        setContentView(R.layout.activity_main);

        crono1= (Chronometer)findViewById(R.id.crono1);

        crono1.setBase(SystemClock.elapsedRealtime());

        crono1.start();

        btEsquerda = (Button)findViewById(R.id.button1);

        btEsquerda.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    outputStream = null;
                    outputStream = meuSocket.getOutputStream();
                    outputStream.write('a');

                    Toast.makeText(getApplicationContext(), "Mensagem foi enviada", Toast.LENGTH_LONG).show();
                } catch(IOException e){
                    Toast.makeText(getApplicationContext(), "Mensagem não recebida", Toast.LENGTH_LONG).show();

                }
            }
        });
        btDireita = (Button)findViewById(R.id.button2);

        btDireita.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    outputStream = null;
                    outputStream = meuSocket.getOutputStream();
                    outputStream.write('b');

                    Toast.makeText(getApplicationContext(), "Mensagem foi enviada", Toast.LENGTH_LONG).show();
                } catch(IOException e){
                    Toast.makeText(getApplicationContext(), "Mensagem não recebida", Toast.LENGTH_LONG).show();

                }
            }
        });
        try{
            while(true){

                inputStream = meuSocket.getInputStream();
                serial = (char) inputStream.read();

                if(serial == 'a'){
                    chamaPQP();
                }
                else if(serial == 'b'){
                    chamaTNC();
                }
                else{
                     Toast.makeText(getApplicationContext(), "Manda outro", Toast.LENGTH_SHORT).show();
                }
         }}
        catch(Exception e){}

    }

    private void chamaTNC() {
        setContentView(R.layout.tnc);

    }

    private void chamaPQP() {
        setContentView(R.layout.pqp);

    }

    public void conecta(){
        if(BluetoothAdapter.checkBluetoothAddress(MAC)){
            dispositivoBluetoohRemoto = bluetooth.getRemoteDevice(MAC);
        } else{ 
            Toast.makeText(getApplicationContext(), "Endereço MAC do dispositivo Bluetooth remoto não é válido", Toast.LENGTH_SHORT).show();
        }

        try{
            meuSocket = dispositivoBluetoohRemoto.createRfcommSocketToServiceRecord(MEU_UUID);

            meuSocket.connect();

            Toast.makeText(getApplicationContext(), "Conectado", Toast.LENGTH_SHORT).show();

        } catch(IOException e){
            Toast.makeText(getApplicationContext(), "Conexão não foi estabelecida", Toast.LENGTH_SHORT).show();
        }
    }       
}
  • William, please translate your question into Portuguese and explain the problem you are having. Putting code without explanation does not lead anywhere...

  • I can’t get you to perform all these tasks at once, only perform the rest when you receive some data in inputStream. And after receiving this data he no longer receives any

  • Can [Dit] the question and put this info pf? otherwise the question is really closed.

  • Help me please

  • 5

    package com.vaiporra;, private void chamaPQP(), private void chamaTNC() ... :)

  • The InputStream that goes wrong is what’s inside the while? If yes recommend putting something on catch for if an error occurs, it will at least show you that something went wrong.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.