Is it possible to send values of the gyroscope and a seekbar simultaneously via bluetooth?

1

Hello, everyone!

I'm trying to control a Drone (Hubsan X4) via smartphone sensors. But I can not send the collected values of the gyroscope and a seekbar simultaneously. The seekbar represents the speed (throttle) and the gyro is the directional control of the drone.

I can only send one of the data (either gyroscope or seekbar), when implementing both codes bluetooth sends only the data of the gyroscope that is in the onSensorChange () and the programming of the seekbar is in onCreate (). >

Could anyone help me with this?

Thanks in advance for your attention!

    
asked by anonymous 10.04.2015 / 03:37

1 answer

1

@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);
                }

            }

        }

    }

}
    
18.04.2015 / 21:42