1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
public class ConductorLink extends Activity implements SensorListener {
private SensorManager sensorManager;
private ConnectionClient client;
private TextView test;
private float Accelerometer[] = new float[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.connect);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
test = (TextView) findViewById(R.id.TextViewSense);
Button connectButton = (Button) findViewById(R.id.ButtonConnect);
connectButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String ipStr = ((EditText) findViewById(R.id.EditTextIP))
.getText().toString();
try {
int port = Integer
.parseInt(((EditText) findViewById(R.id.EditTextPort))
.getText().toString());
client = new ConnectionClient(ipStr, port);
client.sendMessageToServer("success");
} catch (NumberFormatException e) {
Toast.makeText(ConductorLink.this, e.toString(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(ConductorLink.this, e.toString(),
Toast.LENGTH_SHORT).show();
}
}
});
}
protected void onResume() {
sensorManager.registerListener(this,
SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_UI);
super.onResume();
}
@Override
protected void onPause() {
sensorManager.unregisterListener(this);
super.onPause();
}
public void onAccuracyChanged(int arg0, int arg1) {
}
public void onSensorChanged(int sensor, float[] values) {
if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
Accelerometer[0] = values[SensorManager.DATA_X];
Accelerometer[1] = values[SensorManager.DATA_Y];
Accelerometer[2] = values[SensorManager.DATA_Z];
test.setText(String.format("%.2f %.2f %.2f", Accelerometer[0],Accelerometer[1], Accelerometer[2]));
client.sendMessageToServer(String.format("X:%.2f Y:%.2f Z:%.2f", Accelerometer[0],Accelerometer[1], Accelerometer[2]));
}
}
}
|