@Daniela, I was sending as you suggested (one at a time), however as the sending of the sensor data were on the onSensorChange () and sending the seekbar data was onCreate () could not send them simultaneously. Since I'm still new to this type of development I did not know I could not put everything in onOnSensorChange (). But I can :) Finally, I concatenated all the values in a string, converted to a vector bytes and now it works perfectly :) See the code:
@Override
public void onSensorChanged(SensorEvent event) {
byte[] vetor;
String eixoX, eixoY, eixoZ; // em GRAUS.
String pacote = "";
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
gravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
geomagnetic = event.values;
if (gravity != null && geomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean sucesso = SensorManager.getRotationMatrix(R, I, gravity,
geomagnetic);
if (sucesso) {
float orientation[] = new float[3];
orientation = SensorManager.getOrientation(R, orientation);
eixoX = (int) Math.toDegrees(orientation[0]);
eixoY = (int) Math.toDegrees(orientation[1]);
eixoZ = (int) Math.toDegrees(orientation[2]);
// Envia para o arduíno
if (mConnectedThread != null) {
try {
// INÍCIO - SEEKBAR
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// velocidade.setText(progresso);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progressValue, boolean fromUser) {
// sensorStop = true;
velocidade.setText(Integer
.toString(progressValue));
}
});
// FIM - SEEKBAR
// Velocidade
pacote += "V" + velocidade.getText() + "|";
// Envia
pacote += "X" + eixoX + "|";
// Envia Y
pacote += "Y" + eixoY + "|";
// Envia
pacote += "Z" + eixoZ + "|";
mConnectedThread.write(pacote.getBytes());
Thread.sleep(20);
} catch (Exception ex) {
Toast.makeText(this,
"Erro ao enviar os dados via bluetooth",
Toast.LENGTH_LONG);
}
}
}
}
}@Override
public void onSensorChanged(SensorEvent event) {
byte[] vetor;
int eixoX, eixoY, eixoZ; // em GRAUS.
String valorX, valorY, valorZ; // em BYTES.
String pacote = "";
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
gravity = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
geomagnetic = event.values;
if (gravity != null && geomagnetic != null) {
float R[] = new float[9];
float I[] = new float[9];
boolean sucesso = SensorManager.getRotationMatrix(R, I, gravity,
geomagnetic);
if (sucesso) {
float orientation[] = new float[3];
orientation = SensorManager.getOrientation(R, orientation);
eixoX = (int) Math.toDegrees(orientation[0]);
eixoY = (int) Math.toDegrees(orientation[1]);
eixoZ = (int) Math.toDegrees(orientation[2]);
xCoor.setText("X: " + eixoX);
yCoor.setText("Y: " + eixoY);
zCoor.setText("Z: " + eixoZ);
// Envia para o arduíno
if (mConnectedThread != null) {
try {
// INÍCIO - SEEKBAR
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// velocidade.setText(progresso);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progressValue, boolean fromUser) {
// sensorStop = true;
velocidade.setText(Integer
.toString(progressValue));
}
});
// FIM - SEEKBAR
// Velocidade
pacote += "V" + velocidade.getText() + "|";
// Envia X
//valorX = Integer.toString(eixoX * 256 / 360);
//pacote += "X" + valorX + "|";
// Envia Y
valorY = Integer.toString(eixoY * 256 / 360);
pacote += "Y" + valorY + "|";
// Envia Z
valorZ = Integer.toString(eixoZ * 256 / 360);
if (Integer.parseInt(valorZ) > 50)
valorZ = Integer.toString(50);
else if (Integer.parseInt(valorZ) < 0)
valorZ = Integer.toString(0);
pacote += "Z" + valorZ + "|";
mConnectedThread.write(pacote.getBytes());
Thread.sleep(20);
} catch (Exception ex) {
Toast.makeText(this,
"Erro ao enviar os dados via bluetooth",
Toast.LENGTH_LONG);
}
}
}
}
}