I can not get it to execute all these tasks at the same time, it only executes the rest when it receives some data in InputStream
. And after receiving this data he does not receive any more. Help me please, I'm newbie and I've tried several examples but I can not get it to 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();
}
}
}