public class MainActivity extends AppCompatActivity {
TextView textView;
TextView textView2;
SensorManager manager;
List<Sensor> sensors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager = (SensorManager)getSystemService(SENSOR_SERVICE);
sensors = manager.getSensorList(Sensor.TYPE_ALL);
int index = 0;
for (Sensor sensor : sensors) {
textView.append("#" + index++ + ": " + sensor.getName() + '\n');
}
}
});
final SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
String str = "Sensor Timestamp: " + event.timestamp + "\n\n";
str = str + "Sensor Accuracy: " + event.accuracy + '\n';
for (int index = 0; index < event.values.length; index++) {
str += ("Sensor Value #" + index + ": " + event.values[index] + '\n');
}
textView.setText(str);
}
if (event.sensor.getType() == Sensor.TYPE_AMBIENT_TEMPERATURE) {
String str = "Sensor Timestamp: " + event.timestamp + "\n\n";
str = str + "Sensor Accuracy: " + event.accuracy + '\n';
for (int index = 0; index < event.values.length; index++) {
str += ("Sensor Value #" + index + ": " + event.values[index] + '\n');
}
textView2.setText(str);
}
}
/*
SENSOR_STATUS_ACCURACY_HIGH
This sensor is reporting data with maximum accuracy
Constant Value: 3 (0x00000003)
SENSOR_STATUS_ACCURACY_LOW
This sensor is reporting data with low accuracy, calibration with the environment is needed
Constant Value: 1 (0x00000001)
SENSOR_STATUS_ACCURACY_MEDIUM
This sensor is reporting data with an average level of accuracy, calibration with the environment may improve the readings
Constant Value: 2 (0x00000002)
SENSOR_STATUS_NO_CONTACT
The values returned by this sensor cannot be trusted because the sensor had no contact with what it was measuring
(for example, the heart rate monitor is not in contact with the user).
Constant Value: -1 (0xffffffff)
SENSOR_STATUS_UNRELIABLE
The values returned by this sensor cannot be trusted, calibration is needed or the environment doesn't allow readings
Constant Value: 0 (0x00000000)
*/
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
Button button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
manager.registerListener(sensorEventListener, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
// If sensors.get(0) == manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)...
//manager.registerListener(sensorEventListener, sensors.get(0), SensorManager.SENSOR_DELAY_UI);
manager.registerListener(sensorEventListener, manager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE), SensorManager.SENSOR_DELAY_UI);
}
});
Button button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
manager.unregisterListener(sensorEventListener, manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
// If sensors.get(0) == manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)...
//manager.unregisterListener(sensorEventListener, sensors.get(0));
manager.unregisterListener(sensorEventListener, manager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE));
}
});
}
}