I want to collect Accelerometer,Accelerometer (Without Gravity) and Gyroscope data from my phone and send it to a TCP socket in my PC. It send the values correctly, but that values are wrong. All of them are 0.0.
In the photo I have the sensor data Output. Always 0.
Here is the code of the MainActivity and One sensor(the other sensors are the same just changed the Sensor.TYPE_...)
MainActivity Code:
public class MainActivity extends AppCompatActivity {
EditText IP_entrada;
TextView ConsoleOut;
TextView ConsoleDebug;
String Salida = "CONSOLE OUTPUT";
boolean Cerrar;
boolean Conectado;
Socket sk;
PrintWriter TCPOutput;
BufferedReader TCPInput;
String ACK = "";
private Accelerometer accelerometer;
private AccelerometerNG accelerometerNG;
private Gyroscope gyroscope;
float[] data = new float[9];
private String Sdata;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IP_entrada = findViewById(R.id.IP_text);
ConsoleOut = findViewById(R.id.text_accelerometer);
ConsoleDebug = findViewById(R.id.Output);
Cerrar = false;
Conectado = false;
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
accelerometer = new Accelerometer(this);
accelerometerNG = new AccelerometerNG(this);
gyroscope = new Gyroscope(this);
accelerometer.setListener(new Accelerometer.Listener() {
@Override
public void onTranslation(float rx, float ry, float rz) {
data[0] = rx;
data[1] = ry;
data[2] = rz;
}
});
accelerometerNG.setListener(new AccelerometerNG.Listener() {
@Override
public void onTranslationNG(float rx, float ry, float rz) {
data[3] = rx;
data[4] = ry;
data[5] = rz;
}
});
gyroscope.setListener(new Gyroscope.Listener() {
@Override
public void onRotation(float rx, float ry, float rz) {
data[6] = rx;
data[7] = ry;
data[8] = rz;
}
});
}
private void Send(String _DATA) {
TCPOutput.println(_DATA);
}
private void Read(){
try {
ACK = TCPInput.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
public void SetDirection(View view) {
log("Modo Conectar = true");
try {
log(" Socket: " + IP_entrada.getText().toString() + ":" + 8080);
sk = new Socket(IP_entrada.getText().toString(), 8080);
log("Conectado a IP:"+IP_entrada.getText().toString());
TCPOutput = new PrintWriter(new OutputStreamWriter(sk.getOutputStream()), true);
log("Conectado con exito");
TCPInput = new BufferedReader(new InputStreamReader(sk.getInputStream()));
Conectado = true;
} catch (IOException e) {
e.printStackTrace();
}
}
public void DisconnectClient(View view){
log("Modo Cerrar = true");
Cerrar = true;
}
private void log(String string) {
ConsoleDebug.setText(String.format("%s\n", string));
}
@Override
protected void onResume(){
super.onResume();
accelerometer.register();
accelerometerNG.register();
gyroscope.register();
Sdata = data[0]+","+data[1]+","+data[2]+","+data[3]+","+data[4]+","+data[5]+","+data[6]+","+data[7]+","+data[8];
ConsoleOut.setText((data[0]+" "+data[1]+" "+data[2]+"\n"+data[3]+" "+data[4]+" "+data[5]+"\n"+data[6]+" "+data[7]+" "+data[8]));
if(Conectado){
Send(Sdata);
do{
Read();
}while (!ACK.equals("ACKDAT"));
}
if(Cerrar){
try {
sk.close();
} catch (IOException e) {
e.printStackTrace();
}
Conectado = false;
}
}
@Override
protected void onPause(){
super.onPause();
accelerometer.unregister();
accelerometerNG.unregister();
gyroscope.unregister();
}
}
And the Gyroscope class:
public class Gyroscope {
public interface Listener{
void onRotation(float rx, float ry, float rz);
}
private Listener listener;
public void setListener(Listener l){
listener = l;
}
private SensorManager sensorManager;
private Sensor sensor;
private SensorEventListener sensorEventListener;
Gyroscope(Context context){
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
if(listener != null){
listener.onRotation(sensorEvent.values[0],sensorEvent.values[1],sensorEvent.values[2]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
}
public void register(){
sensorManager.registerListener(sensorEventListener,sensor,SensorManager.SENSOR_DELAY_NORMAL);
}
public void unregister(){
sensorManager.unregisterListener(sensorEventListener);
}
}
I also put the classes that I have and added the AndroidManifest.xml lines.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
<uses-feature android:name="android.hardware.sensor.gyroscope" android:required="true" />
What is wrong and what can i do?
Try cleaning and recompiling the project.