Get multiple sensor data 여러가지 센서 데이터 받아 오기
Android 2019. 10. 15. 20:37 |반응형
You can retrieve multiple sensor data at the same time. Say you need ACCELEROMETER and AMBIENT_TEMPERATURE data.
동시에 여러가지 센서 데이터를 받아 올 수 있다. 예를 들어 ACCELEROMETER 와 AMBIENT_TEMPERATURE 센서의 데이터를 받아 오자.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | 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)); } }); } } |
Sensor List
Sensor data
반응형
'Android' 카테고리의 다른 글
Take a photo with a camera app and get the thumbnail (0) | 2019.10.18 |
---|---|
Get contact data 연락처 정보 가져오기 (0) | 2019.10.16 |
Character encoding simple check 간단한 한글 인코딩 확인 (0) | 2019.10.12 |
Data request to the web server 웹서버에 데이터 요청하고 응답 받기 (0) | 2019.10.12 |
How to use Timer and TimerTask 타이머 사용하기 (0) | 2019.10.09 |